Python查找列表中的最大索引 [英] Python Finding Index of Maximum in List

查看:188
本文介绍了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()

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

I am able to get the maximum value in the array (without using max() of course...). How can I get the index (position) of that value? Please try to keep it simple without using new Python key words or built-in functions. Thanks!

推荐答案

如果不允许使用内置的index()函数,只需使用索引进行迭代,而不是使用foreach循环。

If you aren't allowed to use the built in index() function, just iterate with an index, instead of using a foreach loop.

for i in range(len(a)):
    if a[i] > max:
        max = a[i]
        maxIndex = i

这篇关于Python查找列表中的最大索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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