[英]Find the smallest number in a python list and print the position
I have a list of integer imported via a file我有一个通过文件导入的整数列表
xy = [50, 2, 34, 6, 4, 3, 1, 5, 2]
I am aware of Python: finding lowest integer我知道Python:找到最小的整数
However, I wonder how can I print the position of it instead of just finding the smallest number?但是,我想知道如何打印它的位置而不仅仅是找到最小的数字?
Just use the list.index
method:只需使用
list.index
方法:
print xy.index(min(xy))
# 6
If the minimum is repeated, you'll only get the index of the first occurrence, though.但是,如果重复最小值,则只会获得第一次出现的索引。
indices = [i for i, x in enumerate(xy) if x == min(xy)] # Indices of all min occurrences
Just in case someone wishes to use for loop:以防万一有人希望使用 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.