簡體   English   中英

在列表列表中查找每個列表的最小值的索引

[英]Finding the index of the minimum of each list in a list of lists

我正在嘗試創建列表列表中每個列表的最小值的索引列表。 我只能找到一個簡單列表的答案。

data = [[9 ,5, 2, 8, 6], [3, 5, 1, 9, 2], [2, 9, 3, 0, 5]]

我的第一個想法是使用

.index(min(n))    

但不適用於列表列表。

預期結果:

new_list = [2, 2, 3]

使用列表理解:

[x.index(min(x)) for x in data]
     >>>data = [[9 ,5, 2, 8, 6], [3, 5, 1, 9, 2], [2, 9, 3, 0, 5]]
     >>>[x.index(min(x))+1 for x in data]

      [3, 3, 4] //actual index (Your required output)

試試吧:

result = []
for list in data:
    result.append(list.index(min(list)))

同時,您想要得到的答案是[2,2,3] ,而不是[3,3,4] 因為列表的索引從0開始。 希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM