在字符串列表中找到所有子字符串,并创建一个新的匹配子字符串列表.在Python中 [英] Find all substrings in list of strings and create a new list of matching substrings. in Python

查看:269
本文介绍了在字符串列表中找到所有子字符串,并创建一个新的匹配子字符串列表.在Python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个子字符串列表和一个字符串列表.我想在字符串列表中找到所有匹配的子字符串.当在字符串中找到子字符串时,我想创建一个新的字符串列表,其中包含在每个字符串中找到的所有子字符串匹配项.

I have a list of substrings and a list of strings. I would like to find all matching substrings in the list of strings. When substrings are found in the strings I would like to create a new list of strings containing all substring matches found in each string.

例如,假设我有这些东西:

For example let's say I have these:

substrings = ["word","test"]

strings = ["word string one", "string two test", "word and test", "no matches in this string"]

我创建了以下内容以使子字符串与字符串匹配:

I have created the following to match the substrings with the string:

for s in strings:
for k in substrings:
    if k in s:
        print(k)

这将提供以下输出:

word
test
word
test 

我也尝试了以下方法:

matches = [x for string in strings for x in string.split() if x in substrings]
print (matches)

输出:

['word', 'test', 'word', 'test']

这些结果都不是我所追求的.由于单词"和测试"都出现在第三个字符串中,因此我想得到与以下任一输出类似的内容:

None of these results are what I am after. As both "word" and "test" occur in the third string I would like to get something similar to either of the following outputs:

word
test
word, test 

['word', 'test', 'word test']

推荐答案

您的代码没有给您想要的结果,因为您没有将多个匹配项一起保留在自己的列表中.

Your code isn't giving you the result you want because you are not keeping multiple matches together in their own list.

实现所需内容的最简单方法是在循环中保留另一个列表,以包含与当前字符串匹配的子字符串.

The easiest way of achieving what you are looking for is to keep another list inside the loop to contain substrings that matches the current string.

substrings = ["word","test"]

strings = ["word string one",
           "string two test",
           "word and test",
           "no matches in this string"]

result = []    

for string in strings:
    matches = []
    for substring in substrings:
        if substring in string:
            matches.append(substring)
    if matches:
        result.append(matches)

这应该给你

[['word'], ['test'], ['word', 'test']]

如果您想以问题中指出的格式实际打印这些内容,只需更改

If you want to actually print these in the format you stated in your question, simply change

result.append(matches)

print(' '.join(matches))

这将为您提供:

word
test
word test

这篇关于在字符串列表中找到所有子字符串,并创建一个新的匹配子字符串列表.在Python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆