a = [2, 2, 4, 2, 5, 7]
如果要查找此列表中的最小值(2
),列表2
中a
的相应索引分别为0,1和3。
如何告诉python使用最高索引处的最小值(索引3处的数字2
)?
答案 0 :(得分:4)
您可以使用a[::-1]
简单地反转列表,然后应用相同的技术来查找索引。 a[::-1].index(min(a))
将为我们提供最终值的索引,为了将索引记录到第0个位置(列表的开头),我们可以从a[::-1].index(min(a))
中减去len(a) - 1
。
a = [2, 2, 4, 2, 5, 7]
print len(a) - a[::-1].index(min(a)) - 1
>>> 3
答案 1 :(得分:2)
这可以写成函数max()
,然后获取生成的迭代器的def indexes(xs, value):
for i, x in enumerate(xs):
if x == value:
yield i
a = [2, 2, 4, 2, 5, 7]
print max(indexes(a, min(a))) # 3
:
示例:强>
max()
更新; 就完整性而言;如果您本身想要一组具有多个最小值的值的最小索引,那么您只需在表达式的前面换出min()
a = [2, 2, 1, 4, 2, 1, 5, 7]
print min(indexes(a, min(a))) # 2
。所以:
print max(indexes(a, min(a))) # 5
同时
$nom = DB::table('laureats')->pluck('nom');
这通常很方便灵活:)
答案 2 :(得分:2)
>>> -min(zip(a, range(0, -len(a), -1)))[1]
3
答案 3 :(得分:2)
使用枚举反转列表,其中起始索引为$formatCells = $ws1.Range("A1:W$a")
$formatCells.select()
$formatCells.font.size=10
$formatCells.Borders.Item($xledgebottom).Weight = $xlThick
$formatCells.Borders.Item($xledgetop).Weight = $xlThick
$formatCells.Borders.Item($xledgeleft).Weight = $xlThick
$formatCells.Borders.Item($xledgeright).Weight = $xlThick
$formatCells.Borders.Item($xledgebottom).LineStyle = $xlContinuous
$formatCells.Borders.Item($xledgetop).LineStyle = $xlContinuous
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous
$formatCells.Borders.Item($xledgeleft).LineStyle = $xlContinuous
$formatCells = $ws1.Range("A2:W2")
$formatCells.select()
$formatCells.Borders.Item($xledgebottom).Weight = $xlThin
$formatCells.Borders.Item($xledgebottom).LineStyle = $xlContinuous
$formatCells = $ws1.Range("A3:W$a")
$formatCells.select()
$formatCells.Borders.Item($xlinsidehorizontal).LineStyle = $xldot
$formatCells.Borders.Item($xlinsidevertical).LineStyle = $xldot
$formatCells.Borders.Item($xlinsidehorizontal).Weight = $xlhairline
$formatCells.Borders.Item($xlinsidevertical).weight = $xlhairline
$formatCells = $ws1.Range("C1:C$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous
$formatCells = $ws1.Range("F1:F$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous
$formatCells = $ws1.Range("J1:J$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous
$formatCells.Borders.Item($xledgeleft).Weight = $xlThick
$formatCells.Borders.Item($xledgeleft).LineStyle = $xlContinuous
$formatCells = $ws1.Range("N1:N$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous
$formatCells.Borders.Item($xledgeleft).Weight = $xlThick
$formatCells.Borders.Item($xledgeleft).LineStyle = $xlContinuous
$formatCells = $ws1.Range("R1:R$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous
$formatCells.Borders.Item($xledgeleft).Weight = $xlThick
$formatCells.Borders.Item($xledgeleft).LineStyle = $xlContinuous
$formatCells = $ws1.Range("V1:V$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous
$formatCells.Borders.Item($xledgeleft).Weight = $xlThick
$formatCells.Borders.Item($xledgeleft).LineStyle = $xlContinuous
,在生成器中调用next以获取第一个值,从列表长度中减去索引。
1
答案 4 :(得分:0)
遍历枚举列表并使用最大索引:
In [109]: max([ind for ind, val in enumerate(a) if val==min(a)])
Out[109]: 3
答案 5 :(得分:0)
找到最小的,然后遍历列表,收集匹配值的索引。
smallest = min(items)
indices = [index for index, value in enumerate(items) if value == smallest]
largest_index = max(indices) # or indices[-1]
如果您的列表非常长,那么您可能需要从后面开始并在到达第一个项目后停止。
largest_index = len(items) - next(index for index, value in \
enumerate(reversed(items), start=1) if value == smallest)
答案 6 :(得分:0)
这可能会:
(len(a) -1 )- a[: : -1].index(min(a))
答案 7 :(得分:0)
试试这个:
a=[2,2,4,2,5,7]
minval=min(a) #min of a
for l in range(len(a)):
if a[l]==minval:
index=l
continue
print index