Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List - Fatal编程技术网

Python 检查单词列表中是否有模式

Python 检查单词列表中是否有模式,python,list,Python,List,我需要一个输出,其中包含的字,完全像一个模式-相同的字母在相同的地点只有(和字母不应该显示在字在其他地方)和相同的长度 例如: words = ['hatch','catch','match','chat','mates'] pattern = '_atc_ 所需产出: ['hatch','match'] 我曾尝试使用嵌套for循环,但对于以“\u1”开头和结尾的模式无效 def filter_words_list(words, pattern): relevant_words = []

我需要一个输出,其中包含的字,完全像一个模式-相同的字母在相同的地点只有(和字母不应该显示在字在其他地方)和相同的长度 例如:

words = ['hatch','catch','match','chat','mates'] 
pattern = '_atc_
所需产出:

['hatch','match']
我曾尝试使用嵌套for循环,但对于以“\u1”开头和结尾的模式无效

def filter_words_list(words, pattern):
relevant_words = []
for word in words:
    if len(word) == len(pattern):
        for i in range(len(word)):
            for j in range(len(pattern)):
                if word[i] != pattern[i]:
                    break
                if word[i] == pattern[i]:
                    relevant_words.append(word)
谢谢

您可以使用:

输出

['hatch', 'match']
['hatch', 'match']
模式
'[^atc]atc[^atc]'
匹配所有非a或t或c的内容(
[^atc]
),后跟
'atc'
,然后再匹配所有非a或t或c的内容

作为替代方案,您可以编写自己的匹配函数,用于任何给定模式:

from collections import Counter


def full_match(word, pattern='_atc_'):
    if len(pattern) != len(word):
        return False

    pattern_letter_counts = Counter(e for e in pattern if e != '_')  # count characters that are not wild card
    word_letter_counts = Counter(word) # count letters

    if any(count != word_letter_counts.get(ch, 0) for ch, count in pattern_letter_counts.items()):
        return False

    return all(p == w for p, w in zip(pattern, word) if p != '_')  # the word must match in all characters that are not wild card


words = ['hatch', 'catch', 'match', 'chat', 'mates']


result = list(filter(full_match, words))
print(result)
输出

['hatch', 'match']
['hatch', 'match']
进一步

  • 请参阅有关内置函数和的文档
  • 请参阅上的文档
  • 您可以使用:

    输出

    ['hatch', 'match']
    
    ['hatch', 'match']
    
    模式
    '[^atc]atc[^atc]'
    匹配所有非a或t或c的内容(
    [^atc]
    ),后跟
    'atc'
    ,然后再匹配所有非a或t或c的内容

    作为替代方案,您可以编写自己的匹配函数,用于任何给定模式:

    from collections import Counter
    
    
    def full_match(word, pattern='_atc_'):
        if len(pattern) != len(word):
            return False
    
        pattern_letter_counts = Counter(e for e in pattern if e != '_')  # count characters that are not wild card
        word_letter_counts = Counter(word) # count letters
    
        if any(count != word_letter_counts.get(ch, 0) for ch, count in pattern_letter_counts.items()):
            return False
    
        return all(p == w for p, w in zip(pattern, word) if p != '_')  # the word must match in all characters that are not wild card
    
    
    words = ['hatch', 'catch', 'match', 'chat', 'mates']
    
    
    result = list(filter(full_match, words))
    print(result)
    
    输出

    ['hatch', 'match']
    
    ['hatch', 'match']
    
    进一步

  • 请参阅有关内置函数和的文档
  • 请参阅上的文档

  • 所以你应该使用正则表达式。并将下划线替换为“.”,表示任何单个字符。 因此,输入看起来像:

    words = ['hatch','catch','match','chat','mates'] 
    pattern = '.atc.'
    
    代码是:

    import re
    def filter_words_list(words, pattern):
    ret = []
    for word in words:
        if(re.match(pattern,word)):ret.append(word)
    return ret
    

    希望这有帮助,所以你应该使用正则表达式。并将下划线替换为“.”,表示任何单个字符。 因此,输入看起来像:

    words = ['hatch','catch','match','chat','mates'] 
    pattern = '.atc.'
    
    代码是:

    import re
    def filter_words_list(words, pattern):
    ret = []
    for word in words:
        if(re.match(pattern,word)):ret.append(word)
    return ret
    

    希望这有帮助

    只要用“.”替换“uu”,然后用你的模式作为常规表达你所说的替换是什么意思?模式是给定的。有一种用python编写模式的标准方法。但是,您的模式使用的是另一种语法“389;”。因此,您只需将语法转换为标准语法,然后就可以使用标准模式匹配库,请参阅。。。如Daniel Masejojojust的答案所示,将“uu”替换为“.”,然后将您的模式用作常规表达式替换是什么意思?模式是给定的。有一种用python编写模式的标准方法。但是,您的模式使用的是另一种语法“389;”。因此,您只需将语法转换为标准语法,然后就可以使用标准模式匹配库,请参阅。。。如Daniel Masejo的回答所示