Python:在数组中找到元素的位置 [英] Python: find position of element in array

查看:5805
本文介绍了Python:在数组中找到元素的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张CSV,其中包含气象站的最高和最低温度,降水量,经度和纬度等天气数据。每类数据都存储在一列中。

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.

我想找到最高和最低温度的位置。找到最大值或最小值很简单:
numpy.min(my_temperatures_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?

这是我的尝试:

def coldest_location(data):

coldest_temp= numpy.min(mean_temp)

    for i in mean_temp:
         if mean_temp[i] == -24.6:
            print i




错误:列表索引必须为int

Error: List indices must be int

我将CSV的每一列保存到变量中,因此它们都是单独的列表。

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 

我已经解决了这个问题根据建议list.index(x)

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?

推荐答案

您是否考虑过使用Python list .index(value) 方法?它返回传入的第一个实例的列表中的索引。

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.

这篇关于Python:在数组中找到元素的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆