>>> birds = ['duck', 'chicken', 'goose']
>>> cats = ['tiger', 'lion']
>>> humans = ['human']
>>> at_the_zoo = [birds, cats, humans]
给出像at_the_zoo这样的列表列表,如何找到老虎所在的列表?
for animal in sum(at_the_zoo, []):
if "tiger" == animal:
print "1 help!"
例如,我可以在动物列表中找到老虎,如果我使用枚举,它会告诉我它在索引3处。我怎么知道它是列表at_the_zoo的元素1的一部分。 寻找鸭子会告诉我元素0等。
谢谢!
答案 0 :(得分:4)
我会想到:
def find_element(nested_lst, what):
for idx, sublst in enumerate(nested_lst):
try:
idx2 = sublst.index(what)
return (idx, idx2)
except ValueError:
pass
应该有用。
示例:
>>> def find_element(nested_lst, what):
... for idx, sublst in enumerate(nested_lst):
... try:
... idx2 = sublst.index(what)
... return (idx, idx2)
... except ValueError:
... pass
...
>>> birds = ['duck', 'chicken', 'goose']
>>> cats = ['tiger', 'lion']
>>> humans = ['human']
>>> find_element([birds, cats, humans], 'human')
(2, 0)
>>> find_element([birds, cats, humans], 'gator') # returns None if not found.
>>> find_element([birds, cats, humans], 'tiger')
(1, 0)
值得注意的是,平均而言,list.index
是O(N)操作,这意味着列表不是用于测试成员资格的最有效数据结构。如果您的实际数据支持它,则可能值得考虑使用set
代替。
答案 1 :(得分:3)
只需建立一个索引:
>>> birds = ['duck', 'chicken', 'goose']
>>> cats = ['tiger', 'lion']
>>> humans = ['human']
>>> at_the_zoo = [birds, cats, humans]
>>> index = {}
>>> for i, arr in enumerate(at_the_zoo):
... index.update(zip(arr, [i]*len(arr)))
...
>>> index
{'tiger': 1, 'goose': 0, 'lion': 1, 'human': 2, 'duck': 0, 'chicken': 0}
>>> index.get('tiger')
1
>>>
或者:
>>> for i, arr in enumerate(at_the_zoo):
... arr_len = len(arr)
... index.update(zip(arr, zip([i]*arr_len, range(arr_len))))
...
>>> from pprint import pprint
>>> pprint(index)
{'chicken': (0, 1),
'duck': (0, 0),
'goose': (0, 2),
'human': (2, 0),
'lion': (1, 1),
'tiger': (1, 0)}
>>> index.get('tiger')
(1, 0)
答案 2 :(得分:1)
找到了两个已发布的答案,但@ newtover对我的口味有点过于神秘,@ mgilson没有按照要求回答问题。让我了解一下。
def find_in_inner(lst, target):
for i, sublst in enumerate(lst):
if target in sublst:
return i
>>> birds = ['duck', 'chicken', 'goose']
>>> cats = ['tiger', 'lion']
>>> humans = ['human']
>>> at_the_zoo = [birds, cats, humans]
>>> find_in_inner(at_the_zoo, "tiger")
1
答案 3 :(得分:0)
对于列表列表的特定情况,如果您想要子列表编号的元组和索引到该子列表中,您可以执行以下操作:
[(i,j) for i, sl in enumerate(LoL) for j,e in enumerate(sl) if e==tgt]
演示:
>>> birds = ['duck', 'chicken', 'goose']
>>> cats = ['tiger', 'lion']
>>> humans = ['human']
>>> zoo = [birds, cats, humans]
>>> tgt='lion'
>>> [(i,j) for i, sl in enumerate(zoo) for j,e in enumerate(sl) if e==tgt]
[(1, 1)]
正确处理子列表中多个目标的潜在情况。
如果你知道你只关心第一个实例,你可以这样做:
[(i,sl.index(tgt)) for i, sl in enumerate(zoo) if tgt in sl]
如果您不关心消除重复项并且需要快速访问,则可以构建索引:
{e:(i,j) for i, sl in enumerate(zoo) for j, e in enumerate(sl)}
# {'tiger': (1, 0), 'goose': (0, 2), 'lion': (1, 1), 'human': (2, 0), 'duck': (0, 0), 'chicken': (0, 1)}
假设在每种情况下都有名为“bob'”的人类,猫和鸟:
birds = ['duck', 'chicken', 'goose', 'bob']
cats = ['tiger', 'lion','bob']
humans = ['human','bob']
您可以构建一个索引,以这种方式正确处理多个子列表中的多个条目:
index={}
for i, sl in enumerate(zoo):
for j, item in enumerate(sl):
index.setdefault(item, []).append((i,j))
索引变为:
{'tiger': [(1, 0)],
'goose': [(0, 2)],
'lion': [(1, 1)],
'human': [(2, 0)],
'duck': [(0, 0)],
'chicken': [(0, 1)],
'bob': [(0, 3), (1, 2), (2, 1)]}