簡體   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