[英]find all occurences of a string and its indices in nested list python
I have a nested list in the following format: 我有以下格式的嵌套列表:
[['john'],['jack','john','mary'],['howard','john'],['jude']...]
I want to find the first 3 or 5 indices of john that occurs in the nested list(since the list is really long) and return the indices like: (0,0),(1,1),(2,1) or in any format which is advisable. 我想找到嵌套列表中出现的john的前3个或5个索引(因为列表确实很长),然后返回类似(0,0),(1,1),(2,1)或建议使用任何格式。
I'm fairly new to nested list. 我是嵌套列表的新手。 Any help would be much appreciated.
任何帮助将非常感激。
Question 1 : Here is one way using a nested comprehension list. 问题1 :这是使用嵌套理解列表的一种方法。 I will however look if there is a dupe.
但是,我将看看是否有欺骗。
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)]
返回:
[(0, 0), (1, 1), (2, 1)]
Update: Something similar found here Finding the index of an element in nested lists in python . 更新:在这里可以找到类似的东西在python的嵌套列表中查找元素的索引 。 The answer however only takes the first value which could be translated into:
但是,答案仅取第一个值,该值可以转换为:
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)
Question 2: (from comment) 问题2 :(来自评论)
Yes this is quite easy by editing y == 'john'
to: 'john' in y
. 是的,通过
'john' in y
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)]
返回:
[(0, 0), (1, 1), (2, 1)]
Question 3: (from comment) 问题3 :(来自评论)
The most efficient way to get the first N elements is to use pythons library itertools like this: 获取前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)]
返回:
[(0, 0), (1, 1)]
This is also answered here: How to take the first N items from a generator or list in Python? 这在这里也得到了回答: 如何从Python的生成器或列表中获取前N个项目?
Question 3 extended: 问题3扩展:
And say now that you want to take them in chunks of N, then you can do this: 并说出您想将它们分成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)]
[]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.