1. 使用max()和enumerate()和operator.itemgetter()方法联合 x = [ 13 , 21.2 , 17.4 , 16 , 4 ] min_index , min_number = min ( enumerate ( x ), key = operator . itemgetter ( 1 )) # min_index=4, min_number =1 max_index , max_number = max
1. 使用max()和enumerate()和operator.itemgetter()方法联合
x = [13, 21.2, 17.4, 16, 4]min_index, min_number = min(enumerate(x), key=operator.itemgetter(1))
# min_index=4, min_number =1
max_index, max_number = max(enumerate(x), key=operator.itemgetter(1))
# max_index=1, max_number = 21.2
2. enumerate()方法
enumerate()函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标
x = [13, 21.2, 17.4, 16, 4]list(enumerate(x))
# 输出 [(0, 13), (1, 21.2), (2, 17.4), (3, 16), (4, 4)]
3. operator.itemgetter()方法
operator.itemgetter()函数用于获取对象的哪些维的数据,参数为想要取的一些维度序号
x = [3, 2.7, 7.8, 6.1, 4]y = operator.itemgetter(2, 1)
y(x) # 输出 (7.8, 2.7)
a1 = operator.itemgetter(3)
a1(x) # 输出 6.1