简体   繁体   English

在嵌套列表python中查找字符串及其索引的所有出现

[英]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.

相关问题 如何在python中查找字符串中所有出现的子字符串 - How to find all occurences of substring in string, in python Python正则表达式,用于查找具有多个变体的字符串的所有出现 - Python regular expression to find all occurences of a string with multiple variations 如何找到文件python中所有出现的字符串(不区分大小写)? - How to find all occurences of string (case-insensitive) in a file, python? 根据 python 中的查找模式从字符串中获取所有出现 - get all occurences from a string based on find pattern in python Python-在嵌套列表中找到_any_和_all_匹配项,并返回索引 - Python - find _any_ and _all_ matches in a nested list, and return the indices 在Python中扩展字符串中的所有出现 - Extend all occurences in a string in Python 如何在一个字符串中发现一个字符串的多次出现,并以空格分隔的列表返回其位置的索引? - How do I find multiple occurences of a string in a string and return the index of its location in a space-separated list? 如何在Python中构建列表中元素出现的索引的dict? - How to build a dict of indices of occurences of elements in a list in Python? 查找字符串中每个子字符串的所有出现 - Find all Occurences of Every Substring in String 查找不完整文本中所有出现的字符串 - Find all the occurences of a string in an imperfect text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM