返回列表列表中的最小索引 [英] returning index of minimum in a list of lists
问题描述
大家好,
是否有一个简单的python函数来返回列表列表中
最小条目的列表索引?
,即[[3,3,3,3],[3,3,3,1],[3,3,3,3]]返回2,4。
或,同样的问题,但只是一个数字列表,而不是列表清单。
谢谢,
Josh
JJ********@gmail.com 写道:大家好,
是否有一个简单的python函数来返回列表列表中
最小条目的列表索引?
即,[[3,3,3,3],[3,3,3,1],[3,3,3,3]]返回2,4。
或者,相同的问题,但只是对于一个数字列表,而不是一个列表列表。
谢谢,
Josh
在你的例子中,你返回了2,4。你的意思是1,3?
mylist [1] [3]是你访问1的方式。在你的清单列表中。
我不认为这个任务有内置功能,但是你可以用
写一个不到4个代码行很容易。
Le Mercredi 21 Juin 2006 16:54, JJ ******** @ gmail.com écrit*:大家好,
是否有一个简单的python函数返回列表列表中最小条目的列表索引?
即[[3,3,3,3],[3,3,3,1],[3,3,3] ,3]]返回2,4。
或者,相同的问题,但只是一个数字列表,而不是列表列表。
谢谢,
Josh>
在[7]中:min([3,3,1,3])
Out [7]:1
在[ 8]:min([(3,3],[3,3,1,3],[3,3,3]]中e的min(e)
Out [8] :1
问候,
-
_____________
Maric Michaud
_____________
亚里士多德 - www.aristote.info
3 place des tapis
69004里昂
电话:+33 426 880 097
JJ ******** @ gmail.com 写道:大家好,
是否有一个简单的python函数来返回
列表清单中的最小条目?
即,[[3,3,3,3],[3,3,3,1],[3,3,3,3]]返回2,4。
或者,同样的问题,但只是一个数字列表,而不是列表清单。
谢谢,
Josh
一种方法是生成(value,index-in-main-list,
index-in-secondary-list)元组,然后只取最小值。
def f(L):
'''''返回列表列表中第一个最小值的索引。''''''' >
返回分钟(
(n,i,j)
代表i,L2代表枚举(L)
代表j ,枚举中的n(L2)
)[1:]
L = [[3,3,3,3],[3,3,3,1],[3,3] ,3,3]]
打印f(L)#打印(1,3)
注意:在python中(和大多数其他)语言)索引从0开始,所以你的
返回值(2,4)是不正确的。
对于数字列表它'更简单。
L = [3,3,3,1,3,3]
打印min((n,i)for i ,n在枚举中(L))[1]#打印3
希望这有帮助
~Simon
>
Hi all,
Is there a simple python function to return the list index of the
minimum entry in a list of lists?
ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
Or, same question but just for a list of numbers, not a list of lists.
Thanks,
Josh
JJ********@gmail.com wrote:Hi all,
Is there a simple python function to return the list index of the
minimum entry in a list of lists?
ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
Or, same question but just for a list of numbers, not a list of lists.
Thanks,
Josh
In your example, you returned 2, 4. Did you mean 1, 3?
mylist[1][3] is the way you would access the "1" in your list of lists.
I don''t think this task would have a built in function, but you could
write one in less than 4 lines of code easily.
Le Mercredi 21 Juin 2006 16:54, JJ********@gmail.com a écrit*:Hi all,
Is there a simple python function to return the list index of the
minimum entry in a list of lists?
ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
Or, same question but just for a list of numbers, not a list of lists.
Thanks,
Josh
In [7]: min([3, 3, 1, 3])
Out[7]: 1
In [8]: min(min(e) for e in [ [3, 3], [3, 3, 1, 3], [3, 3, 3] ])
Out[8]: 1
regards,
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
JJ********@gmail.com wrote:Hi all,
Is there a simple python function to return the list index of the
minimum entry in a list of lists?
ie, for [[3,3,3,3], [3,3,3,1], [3,3,3,3]] to return 2,4.
Or, same question but just for a list of numbers, not a list of lists.
Thanks,
Josh
One way to do this is to generate (value, index-in-main-list,
index-in-secondary-list) tuples and then just take the minimum.
def f(L):
''''''Return indices of the first minimum value in a list of lists.''''''
return min(
(n, i, j)
for i, L2 in enumerate(L)
for j, n in enumerate(L2)
)[1:]
L = [[3, 3, 3, 3], [3, 3, 3, 1], [3, 3, 3, 3]]
print f(L) # prints (1, 3)
Note: In python (and most other languages) indices begin at 0, so your
return values of (2, 4) wouldn''t be correct.
For a list of numbers it''s simpler.
L = [3, 3, 3, 1, 3, 3]
print min((n, i) for i, n in enumerate(L))[1] # prints 3
Hope this helps
~Simon
这篇关于返回列表列表中的最小索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!