Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: find position of element in array

I have a CSV containing weather data like max and min temperatures, precipitation, longitude and latitude of the weather stations etc. Each category of data is stored in a single column.

I want to find the location of the maximum and minimum temperatures. Finding the max or min is easy: numpy.min(my_temperatures_column)

How can I find the position of where the min or max is located, so I can find the latitude and longitude?

Here is my attempt:

def coldest_location(data):  coldest_temp= numpy.min(mean_temp)      for i in mean_temp:          if mean_temp[i] == -24.6:             print i 

Error: List indices must be int

I saved each of the columns of my CSV into variables, so they are all individual lists.

lat          = [row[0] for row in weather_data]  # latitude long         = [row[1] for row in weather_data]  # longitude mean_temp    = [row[2] for row in weather_data]  # mean temperature  

I have resolved the problem as per the suggestion list.index(x)

mean_temp.index(coldest_temp)   coldest_location=[long[5],lat[5]]  

Unsure if asking a second question within a question is proper, but what if there are two locations with the same minimum temperature? How could I find both and their indices?

like image 207
julesjanker Avatar asked Dec 02 '14 23:12

julesjanker


People also ask

How do you find the position of an element in an array in Python?

In summary, the index() function is the easiest way to find the position of an element within a Python list. Although, this function only returns the index of the first occurrence of the given value.

How do you find the position of an element in an array?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found. The following illustrates the syntax of the indexOf() method.


1 Answers

Have you thought about using Python list's .index(value) method? It return the index in the list of where the first instance of the value passed in is found.

like image 132
Aaron Avatar answered Sep 22 '22 20:09

Aaron