正则表达式中的模式匹配

2024-05-29 05:57:12 发布

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

我在下面创建了一个字典,它的键是值的长度。你知道吗

{4: {'lost', 'lust', 'list', 'last', 'lest', 'blue'}, 5: {'beryl'}, 8: {'blowlamp', 'blimbing', 'bluejays', 'jigsawed'}, 9: {'blistered', 'oospheres', 'blackcaps', 'blastular', 'blotchier', 'troweller'}, 10: {'blancmange', 'blackguard', 'volcanizes'}, 6: {'blague', 'blacks', 'blonde', 'blocks'}, 7: {'blawort', 'blinder', 'blender', 'blonder', 'blunder', 'blander'}}

我想在这本词典中列出一个值列表,这样5个单词的元音就在同一个地方,比如 [迷失,欲望,名单,最后,唯恐],[瞎子,搅拌器,金发者,失策者,平淡者]]

我不知道如何以这种方式得到这份名单。一种我认为可以通过正则表达式的方式,但我在什么基础上匹配?单词的长度可以是任意的,元音可以是任意的。你知道吗

这是一个密码战的问题。https://www.codewars.com/kata/vowel-alternations/train/python 到目前为止,我的方法是在字典中得到相同长度的值,这样我就可以处理这些值。我只是不知道如何在价值观上下功夫。 如果有人能告诉我他们在想什么,什么是最好的方法,那会很有帮助。你知道吗

剩下的代码

mdict={}
rev_multidict = {}

for i in words:
    for sublist in i:
        mdict[sublist] = len(sublist)
    for key, value in mdict.items():
        rev_multidict.setdefault(value, set()).add(key)

for key,value in rev_multidict.items():
    i = rev_multidict[key]
    print(i)

Tags: 方法keyinfor字典value方式rev
3条回答

您可以检查第一个字符串中元音的位置,并生成要匹配的正则表达式字符串。每个字符映射到“[aeiou]”或“.”,这取决于它是否是元音。你为什么用“y”取决于你自己。你知道吗

以下代码是接近它的一种方法的开始:

#!/usr/bin/python
import re

words = 'last lest best list '.split()
words1 = 'blander wigwam blunder slender'.split()

print("word list one: {}".format(words))
print('')
aoa = [re.split('[aeiou]', word) for word in words]
for item in aoa:
    print(item)

print('\n')

print("word list two: {}".format(words1))
print('')
aoa1 = [re.split('[aeiou]', word) for word in words1]
for item in aoa1:
    print(item)

输出为:

word list one: ['last', 'lest', 'best', 'list']

['l', 'st']
['l', 'st']
['b', 'st']
['l', 'st']


word list two: ['blander', 'wigwam', 'blunder', 'slender']

['bl', 'nd', 'r']
['w', 'gw', 'm']
['bl', 'nd', 'r']
['sl', 'nd', 'r']

正则表达式在元音上分裂。如果仔细查看拆分的输出,您会注意到对于应该匹配的单词,对应的列表索引值是相同的。也许你可以遍历这些列表并做一个比较。你知道吗

那就交给你了。。。你知道吗

假设您只想在输出列表包含5个单词时打印:

text = {4: {'lost', 'lust', 'list', 'last', 'lest', 'blue'}, 5: {'beryl'}, 8: {'blowlamp', 'blimbing', 'bluejays', 'jigsawed'}, 9: {'blistered', 'oospheres', 'blackcaps', 'blastular', 'blotchier', 'troweller'}, 10: {'blancmange', 'blackguard', 'volcanizes'}, 6: {'blague', 'blacks', 'blonde', 'blocks'}, 7: {'blawort', 'blinder', 'blender', 'blonder', 'blunder', 'blander'}}
vowels = "aeiou"
for i in range(4,10):
        words = text[i]
        rep_idx = []
        for word in words:
            for letter in vowels:
                if letter in word:
                    idx = word.index(letter)
                    if idx not in rep_idx:
                        word_list = []
                        for word in words:
                            if word[idx] in vowels:
                                word_list.append(word)
                        if len(word_list) == 5:
                            print ("{}, Vowel Index: {}".format(word_list, idx))
                    rep_idx.append(idx)

输出:

>>> 
['lust', 'lest', 'list', 'lost', 'last'], Vowel Index: 1
['blonder', 'blender', 'blinder', 'blander', 'blunder'], Vowel Index: 5
['blistered', 'blackcaps', 'troweller', 'blastular', 'blotchier'], Vowel Index: 2
['blistered', 'troweller', 'oospheres', 'blastular', 'blotchier'], Vowel Index: 7

相关问题 更多 >

    热门问题