简体   繁体   English

检查python系列是否在另一个列表中包含任何字符串

[英]Check if a python series contains any string in another list

I have a series of equal length strings of 24 characters like 我有一系列由24个字符组成的等长字符串,例如

my_series = ['ThisIsASentenceXXXXXXXXX', 'SoIsThisXXXXXXXXXXXXXXXX', 'YouGetThePointXXXXXXXXXX']

And I have a list also with equal length strings of 4 characters like 我也有一个列表,其中包含等长的4个字符的字符串,例如

my_list = ['This', 'XXXX', 'GetT']

I want to compare each entry in my_list with each block of 4 characters in every entry in my_series and return the my_series items that the list string was found in. 我想将my_list中的每个条目与my_series中的每个条目中的4个字符的每个块进行比较,并返回在其中找到列表字符串的my_series项目。

For example for the string 'This' in my_list I would want my_series items 1 and 2 returned and for 'XXXX' my_series items 1,2,3 would be returned. 例如,对于my_list中的字符串'This',我希望返回my_series项目1和2,而对于'XXXX',则要返回my_series项目1,2,3。

The following generator will create a 2 dimensional list. 下面的生成器将创建一个二维列表。 Each list will contain any matches, and it's position will match to the my_list index. 每个列表将包含任何匹配项,并且其位置将匹配my_list索引。

n_list = [[x for x in my_series if item in x] for item in my_list]

Output: 输出:

[['ThisIsASentenceXXXXXXXXX', 'SoIsThisXXXXXXXXXXXXXXXX'], ['ThisIsASentenceXXXXXXXXX', 'SoIsThisXXXXXXXXXXXXXXXX', 'YouGetThePointXXXXXXXXXX'], ['YouGetThePointXXXXXXXXXX']] [['ThisIsASentenceXXXXXXXXX','SoIsThisXXXXXXXXXXXXXXXX'],['ThisIsASentenceXXXXXXXXX','SoIsThisXXXXXXXXXXXXXXXX','YouGetThePointXXXXXXXXXX'],['YouGetThePointXXXXXXXXXX']]

therefore n_list[0] contains matches for my_list[0] etc... I hope this helped you out! 因此n_list[0]包含my_list[0]等的匹配项...希望对您有所帮助!

You need to split each entries in the my_series into 6 chunks: 您需要将my_series每个条目分成6个块:

serires_chunks = [(s[0:4], s[4:8], s[8:12], s[12:16], s[16:20], s[20:24])
                  for s in my_series]

Then you can iterate over this chunks to find the matching items: 然后,您可以遍历此块以找到匹配的项目:

for item in my_list:
    for index, chunks in enumerate(serires_chunks):
        for place, chunk in enumerate(chunks, 1):
            if item == chunk:
                location = my_series[index]
                print("Found '{item}' in '{location}' at {place}".format(**locals()))

You'll obtain: 您将获得:

Found 'This' in 'ThisIsASentenceXXXXXXXXX' at 1
Found 'This' in 'SoIsThisXXXXXXXXXXXXXXXX' at 2
Found 'XXXX' in 'ThisIsASentenceXXXXXXXXX' at 5
Found 'XXXX' in 'ThisIsASentenceXXXXXXXXX' at 6
Found 'XXXX' in 'SoIsThisXXXXXXXXXXXXXXXX' at 3
Found 'XXXX' in 'SoIsThisXXXXXXXXXXXXXXXX' at 4
Found 'XXXX' in 'SoIsThisXXXXXXXXXXXXXXXX' at 5
Found 'XXXX' in 'SoIsThisXXXXXXXXXXXXXXXX' at 6
Found 'XXXX' in 'YouGetThePointXXXXXXXXXX' at 5
Found 'XXXX' in 'YouGetThePointXXXXXXXXXX' at 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [Python]检查列表中的任何字符串是否包含另一个列表中的任何字符串 - [Python]Check if any string in a list is contains any string in another list Python Pandas:检查系列是否包含列表中的字符串 - Python Pandas: check if Series contains a string from list 检查 Series 是否包含列表中的任何元素 - Check if Series contains any element from a list 检查 Python 列表项是否包含另一个列表中的字符串 - Check if a Python list item contains a string inside another list 检查 Python 列表项是否在另一个字符串中包含一个字符串但有条件 - Check if a Python list item contains a string inside another string but with conditions Python-检查字符串是否包含列表中的任何元素 - Python - check if string contains any element from a list 如何检查字符串是否包含 Python 列表中的任何 3 个元素 - How to check if a string contains any 3 element from a list in Python 如何检查 Python 列表是否包含任何字符串作为元素 - How to check whether a Python list contains ANY string as an element 在Python / Pandas中,检查逗号分隔的字符串是否包含列表中的任何值 - In Python/Pandas, Check if a comma separated string contains any value in a list Pandas-如何检查DF行中的字符串列表是否包含另一个DF中的任何串联字符串? - Pandas- How to check if list of strings in DF row contains any of strings in series in another DF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM