在嵌套列表中查找所有出现的字符串及其索引 python
find all occurences of a string and its indices in nested list python
我有一个嵌套列表,格式如下:
[['john'],['jack','john','mary'],['howard','john'],['jude']...]
我想找到出现在嵌套列表中的 john 的前 3 或 5 个索引(因为列表真的很长)和 return 索引如下:
(0,0),(1,1),(2,1) 或任何可取的格式。
我对嵌套列表还很陌生。任何帮助将不胜感激。
问题 1:这是使用嵌套理解列表的一种方法。但是,我会看看是否有骗局。
nested_list = [['john'],['jack','john','mary'],['howard','john'],['jude']]
out = [(ind,ind2) for ind,i in enumerate(nested_list)
for ind2,y in enumerate(i) if y == 'john']
print(out)
Returns: [(0, 0), (1, 1), (2, 1)]
更新: 此处发现类似内容 。然而,答案只采用第一个可以翻译成的值:
out = next(((ind,ind2) for ind,i in enumerate(nested_list)
for ind2,y in enumerate(i) if y == 'john'),None)
print(out) # (0,0)
问题2:(来自评论)
是的,将 y == 'john'
编辑为:'john' in y
.
非常容易
nested_list = [['john xyz'],['jack','john dow','mary'],['howard','john'],['jude']]
out = [(ind,ind2) for ind,i in enumerate(nested_list)
for ind2,y in enumerate(i) if 'john' in y]
print(out)
Returns: [(0, 0), (1, 1), (2, 1)]
问题 3:(来自评论)
获取前 N 个元素的最有效方法是像这样使用 pythons 库 itertools:
import itertools
nested_list = [['john xyz'],['jack','john dow','mary'],['howard','john'],['jude']]
gen = ((ind,ind2) for ind,i in enumerate(nested_list)
for ind2,y in enumerate(i) if 'john' in y)
out = list(itertools.islice(gen, 2)) # <-- Next 2
print(out)
Returns: [(0, 0), (1, 1)]
这里也有回答:How to take the first N items from a generator or list in Python?
问题 3 扩展:
现在说你想把它们分成 N 块,那么你可以这样做:
import itertools
nested_list = [['john xyz'],['jack','john dow','mary'],['howard','john'],['jude']]
gen = ((ind,ind2) for ind,i in enumerate(nested_list)
for ind2,y in enumerate(i) if 'john' in y)
f = lambda x: list(itertools.islice(x, 2)) # Take two elements from generator
print(f(gen)) # calls the lambda function asking for 2 elements from gen
print(f(gen)) # calls the lambda function asking for 2 elements from gen
print(f(gen)) # calls the lambda function asking for 2 elements from gen
Returns:
[(0, 0), (1, 1)]
[(2, 1)]
[]
我有一个嵌套列表,格式如下:
[['john'],['jack','john','mary'],['howard','john'],['jude']...]
我想找到出现在嵌套列表中的 john 的前 3 或 5 个索引(因为列表真的很长)和 return 索引如下: (0,0),(1,1),(2,1) 或任何可取的格式。
我对嵌套列表还很陌生。任何帮助将不胜感激。
问题 1:这是使用嵌套理解列表的一种方法。但是,我会看看是否有骗局。
nested_list = [['john'],['jack','john','mary'],['howard','john'],['jude']]
out = [(ind,ind2) for ind,i in enumerate(nested_list)
for ind2,y in enumerate(i) if y == 'john']
print(out)
Returns: [(0, 0), (1, 1), (2, 1)]
更新: 此处发现类似内容
out = next(((ind,ind2) for ind,i in enumerate(nested_list)
for ind2,y in enumerate(i) if y == 'john'),None)
print(out) # (0,0)
问题2:(来自评论)
是的,将 y == 'john'
编辑为:'john' in y
.
nested_list = [['john xyz'],['jack','john dow','mary'],['howard','john'],['jude']]
out = [(ind,ind2) for ind,i in enumerate(nested_list)
for ind2,y in enumerate(i) if 'john' in y]
print(out)
Returns: [(0, 0), (1, 1), (2, 1)]
问题 3:(来自评论)
获取前 N 个元素的最有效方法是像这样使用 pythons 库 itertools:
import itertools
nested_list = [['john xyz'],['jack','john dow','mary'],['howard','john'],['jude']]
gen = ((ind,ind2) for ind,i in enumerate(nested_list)
for ind2,y in enumerate(i) if 'john' in y)
out = list(itertools.islice(gen, 2)) # <-- Next 2
print(out)
Returns: [(0, 0), (1, 1)]
这里也有回答:How to take the first N items from a generator or list in Python?
问题 3 扩展:
现在说你想把它们分成 N 块,那么你可以这样做:
import itertools
nested_list = [['john xyz'],['jack','john dow','mary'],['howard','john'],['jude']]
gen = ((ind,ind2) for ind,i in enumerate(nested_list)
for ind2,y in enumerate(i) if 'john' in y)
f = lambda x: list(itertools.islice(x, 2)) # Take two elements from generator
print(f(gen)) # calls the lambda function asking for 2 elements from gen
print(f(gen)) # calls the lambda function asking for 2 elements from gen
print(f(gen)) # calls the lambda function asking for 2 elements from gen
Returns:
[(0, 0), (1, 1)]
[(2, 1)]
[]