如何匹配两个字符串列表?

2024-04-20 10:54:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想编写Python代码,使用“letter”列表从“word”中搜索匹配项。 我创建了以下两个列表-单词和字母:

word = ['hello', 'how', 'are', 'you', 'potato']
letter = ['how', 'ell', 'aaa', 'bbb', 'tat', 're']

我想要得到的是以下输出作为一个列表,以及“word”和“letter”列表之间的映射结果。 如果匹配了完整的字符串,结果将返回“True”。 如果部分字符串匹配,结果将返回“True”。 如果字符串的nil部分匹配,结果将返回“False”。你知道吗

word_result = ['True', 'True', 'True', 'False', 'True']

我试着自己使用for loop/if…else/import re,但是没有得到我想要的结果。你知道吗

有人能帮忙吗?你知道吗

非常感谢!你知道吗

我使用以下代码进行了测试,但不起作用:

word = ['hello', 'how', 'are', 'you', 'potato']
letters = ['ell', 'how', 'aaa', 'bbb', 'tat', 're']

def check_match():

    for l in letters:
        if l in word:
            print(l)

print(check_match())

预期结果:

word_result = ['True', 'True', 'True', 'False', 'True']

Tags: 字符串代码reyoufalsetruehello列表
3条回答

我不确定你的预期产出是否正确。 下面是应该有效的代码:

def check_match():
    res = []
    for l in letters:
        for w in word:
            if l in w:
                res.append(True)
                break
        else:
            res.append(False)
    return res

输出:

[True, True, False, False, True]

编辑: 现在我明白你的问题了。。。首先,您将看到不同的输入:

word = ['hello', 'how', 'are', 'you', 'potato']
letter = ['how', 'ell', 'aaa', 'bbb', 'tat', 're'] 

然后使用不同的:

word = ['hello', 'how', 'are', 'you', 'potato']
letters = ['ell', 'how', 'aaa', 'bbb', 'tat']

第二,你要检查来自word的单词是否有来自letters的相应字母。您只需切换循环:

def check_match():
    res = []
    for w in word:
        for l in letters:
            if l in w:
                res.append(True)
                break
        else:
            res.append(False)
    return res

输出:

[True, True, True, False, True]

您对部分匹配没有精确的定义,因此很难完整地回答您的问题。你知道吗

然而,我们可以将这一(缺失的)智慧限制在一个函数中:

def partial_match(word, letter):
    ...

其余逻辑可以通过嵌套循环轻松编写:

words = ['hello', 'how', 'are', 'you', 'potato']
letters = ['ell', 'how', 'aaa', 'bbb', 'tat']

results = []
for word in words:
    result = False
    for letter in letters:
        if word == letter or partial_match(word, letter):
            result = True
            break
    results.append(result)

循环的内部部分是编程中常见的设计模式,Python使用any()原语并重写内部循环作为理解提供了一种快捷方式:

results = []
for word in words:
    result = any(
        word == letter or partial_match(word, letter)
        for letter in letters)
    results.append(result)

或者更紧凑,重写两个循环就是理解:

results = [
    any(
        word == letter or partial_match(word, letter)
        for letter in letters)
    for word in words]

现在让我们关注partial_match(),如果您只想确保letter包含在word中,例如:

  • partial_match('how', 'ow') == True
  • partial_match('how', 'ho') == True
  • partial_match('how', 'o') == True
  • partial_match('how', 'oww') == False
  • partial_match('how', 'wow') == False
  • partial_match('how', 'hoe') == False
  • partial_match('how', 'xxx') == False

然后您可以简单地使用:

def partial_match(word, letter):
    return letter in word

注意到精确匹配(由word == letter描述)也会满足partial_match()的要求,最后@U10 Forward的答案是省略==检查、内联partial_match()和几个重命名。你知道吗

如果您的partial_match()应该是不同的,那么以上所有内容仍然有效,您只需要优化该函数。你知道吗

将列表理解与any一起使用:

word = ['hello', 'how', 'are', 'you', 'potato']
letters = ['ell', 'how', 'aaa', 'bbb', 'tat']

def check_match():
    return [any(x in i for x in letters) for i in word]

print(check_match())

输出:

[True, True, False, False, True]

相关问题 更多 >