相关疑难解决方法(0)

如何在列表中找到最大值的所有位置?

我有一个清单:

a = [32, 37, 28, 30, 37, 25, 27, 24, 35, 55, 23, 31, 55, 21, 40, 18, 50,
             35, 41, 49, 37, 19, 40, 41, 31]
Run Code Online (Sandbox Code Playgroud)

最大元素是55(位置9和12上的两个元素)

我需要找到最大值位于哪个位置.请帮忙.

python list max

128
推荐指数
10
解决办法
27万
查看次数

Python:查找浮点列表中最小项的索引

如何在Python浮动列表中找到最小项的索引?如果它们是整数,我只会这样做:

minIndex = myList.index(min(myList))
Run Code Online (Sandbox Code Playgroud)

但是,对于浮点数列表,我得到以下错误,我假设因为浮点相等比较相当不确定.

ValueError: 0.13417985135 is not in list
Run Code Online (Sandbox Code Playgroud)

现在,我知道我可以简单地滚动列表并比较每个项目,看它是<(min + 0.0000000000001)和>(min - 0.0000000000001),但这有点混乱.是否有更优雅(最好是内置)的方法来查找浮动列表中最小项的索引?

python floating-point list minimum floating-point-precision

41
推荐指数
4
解决办法
8万
查看次数

Python查找列表中最大值的索引

def main():
    a = [2,1,5,234,3,44,7,6,4,5,9,11,12,14,13]
    max = 0
    for number in a:
        if number > max:
            max = number
    print max

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

我能够获得数组中的最大值(当然不使用max() ...).如何获得该值的索引(位置)?请尽量保持简单,不使用新的Python关键字或内置函数.谢谢!

python indexing list max

15
推荐指数
5
解决办法
5万
查看次数

在long vector,python中获得最小值索引的有效方法

我有一长串的经度值(len(Lon)= 420481)和另一个纬度值.我想找到经度最小值的相应纬度.

我试过了:

SE_Lat = [Lat[x] for x,y in enumerate(Lon) if y == min(Lon)]
Run Code Online (Sandbox Code Playgroud)

但这需要很长时间才能完成.

有谁知道更有效的方式?

也许你也有这样的建议:我现在尝试找到与新经度最接近的相应纬度,这不在原始经度向量中.我试过这个:

minDiff = [min(abs(x - lon_new) for x in lons)] # not very quick, but works
[(lat,lon) for lat,lon in izip(lats,lons) if abs(lon-lon_new)==minDiff]
Run Code Online (Sandbox Code Playgroud)

最后一行抛出错误,因为有多个匹配.我现在不知道如何只找到一个值,让我们说第一个.任何帮助是极大的赞赏!

python indexing list minimum latitude-longitude

9
推荐指数
2
解决办法
3734
查看次数