Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查python系列是否包含其他列表中的任何字符串_Python - Fatal编程技术网

检查python系列是否包含其他列表中的任何字符串

检查python系列是否包含其他列表中的任何字符串,python,Python,我有一系列24个字符的等长字符串,如 my_series = ['ThisIsASentenceXXXXXXXXX', 'SoIsThisXXXXXXXXXXXXXXXX', 'YouGetThePointXXXXXXXXXX'] 我有一个列表,也有4个字符的等长字符串,比如 my_list = ['This', 'XXXX', 'GetT'] 我想将my_列表中的每个条目与my_系列中每个条目中的每个4个字符的块进行比较,并返回列表字符串所在的my_系列项目 例如,对于my_列表中的字符串

我有一系列24个字符的等长字符串,如

my_series = ['ThisIsASentenceXXXXXXXXX', 'SoIsThisXXXXXXXXXXXXXXXX', 'YouGetThePointXXXXXXXXXX']
我有一个列表,也有4个字符的等长字符串,比如

my_list = ['This', 'XXXX', 'GetT']
我想将my_列表中的每个条目与my_系列中每个条目中的每个4个字符的块进行比较,并返回列表字符串所在的my_系列项目


例如,对于my_列表中的字符串“This”,我希望返回my_系列项目1和2,对于“XXXX”,将返回my_系列项目1、2、3

以下生成器将创建一个二维列表。每个列表将包含任何匹配项,并且其位置将与my_列表索引匹配

n_list = [[x for x in my_series if item in x] for item in my_list]
输出:

[['ThisisSentencexxxxxxxxx'、['ThisisSentencexxxxxxxxx'、['ThisisSentencexxxxxxxxx'、['ThisisSentencexxxxxxxxxxxxx'、['YouGetThePointxxxxxxxxx']、['YouGetThePointxxxxxxxxxxxxx']

因此
n\u列表[0]
包含
my\u列表[0]
等的匹配项。。。
我希望这对你有帮助

您需要将
my_系列
中的每个条目分为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]
然后,您可以迭代此块以查找匹配的项:

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()))
您将获得:

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

你试过什么来达到这个目的吗?也许您可以为我们提供一个您尝试过的示例以及任何错误消息。否则,请参阅“不会有太多帮助”,但类似于“如果我的_系列[0]中的_列表[0]:我不相信这是OP想要的输出。