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