[英]Find the smallest number in a python list and print the position
我有一個通過文件導入的整數列表
xy = [50, 2, 34, 6, 4, 3, 1, 5, 2]
但是,我想知道如何打印它的位置而不僅僅是找到最小的數字?
只需使用list.index
方法:
print xy.index(min(xy))
# 6
但是,如果重復最小值,則只會獲得第一次出現的索引。
indices = [i for i, x in enumerate(xy) if x == min(xy)] # Indices of all min occurrences
以防萬一有人希望使用 for 循環:
xy = [50, 2, 34, 6, 4, 3, 1, 5, 2]
t=0
for i in range(len(xy)):
if xy[i]<xy[t]:
t=i
print t
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.