## 在列表中寻找最大值及其索引的Pythonic方法？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (36)

```maximum=0
for i,value in enumerate(List):
if value>maximum:
maximum=value
index=i```

`List.index(max(List))`

```max_value = max(my_list)
max_index = my_list.index(max_value)```

```import random
from datetime import datetime
import operator

def explicit(l):
max_val = max(l)
max_idx = l.index(max_val)
return max_idx, max_val

def implicit(l):
max_idx, max_val = max(enumerate(l), key=operator.itemgetter(1))
return max_idx, max_val

if __name__ == "__main__":
from timeit import Timer
t = Timer("explicit(l)", "from __main__ import explicit, implicit; "
"import random; import operator;"
"l = [random.random() for _ in xrange(100)]")
print "Explicit: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)

t = Timer("implicit(l)", "from __main__ import explicit, implicit; "
"import random; import operator;"
"l = [random.random() for _ in xrange(100)]")
print "Implicit: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)```

```Explicit: 8.07 usec/pass
Implicit: 22.86 usec/pass```

```Explicit: 6.80 usec/pass
Implicit: 19.01 usec/pass```

Eli Qiao