繁体   English   中英

给定列表列表,找到列表的索引,其中包含Python中的特定项

[英]Given a list of lists, find the index of the list which contains a particular item in Python

例如,我有一个列表列表:

[[1,2],[3,4],[5,6]]

给定元素3,我需要它所属列表的索引,即1。

列表具有互斥的元素。

这是一个快速的解决方案,尽管它确实建立了一个临时列表:

>>> x = [[1,2],[3,4],[5,6]]
>>> v = 3
>>> [v in y for y in x].index(True)
1
>>>
x = [[1,2],[3,4],[5,6]]

v= 89

t= [ v in y for y in x]

y = t.index(True) if any(t) else -1

print y
for index, value in enumerate(lst):
    if desiredValue in value:
        print index

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM