python使用初始字符列表从其他列表检索完整单词?

python使用初始字符列表从其他列表检索完整单词?,python,string,list,Python,String,List,我试图使用缩写词列表来选择和检索由其初始字符序列标识的对应完整词: shortwords = ['appe', 'kid', 'deve', 'colo', 'armo'] fullwords = ['appearance', 'armour', 'colored', 'developing', 'disagreement', 'kid', 'pony', 'treasure'] 使用一个缩短的单词尝试此正则表达式匹配: import re shortword = 'deve'

我试图使用缩写词列表来选择和检索由其初始字符序列标识的对应完整词:

shortwords = ['appe', 'kid', 'deve', 'colo', 'armo']    

fullwords = ['appearance', 'armour', 'colored', 'developing', 'disagreement', 'kid', 'pony', 'treasure']
使用一个缩短的单词尝试此正则表达式匹配:

import re

shortword = 'deve'

retrieved=filter(lambda i: re.match(r'{}'.format(shortword),i), fullwords)

print(retrieved*)
返回

developing
所以正则表达式匹配是有效的,但问题是如何调整代码以迭代短单词列表并检索完整单词


编辑:解决方案需要保留短词列表中的顺序。

这是一种方法:

shortwords = ['appe', 'deve', 'colo', 'arm', 'pony', 'disa']
fullwords = ['appearance', 'developing', 'colored', 'armour', 'pony', 'disagreement']
        
# Dict comprehension
words = {short:full for short, full in zip(shortwords, fullwords)}

#Solving problem
keys = ['deve','arm','pony']
values = [words[key] for key in keys]
        
print(values)

这是一个经典的键值问题。使用字典,或长期考虑熊猫。

< P> >使用字典< /P>
# Using a dictionary 
test= 'appe is a deve arm'
shortwords = ['appe', 'deve', 'colo', 'arm', 'pony', 'disa']    
fullwords = ['appearance', 'developing', 'colored', 'armour', 'pony', 'disagreement']
#Building the dictionary 
d={}
for i in range(len(shortwords)):
    d[shortwords[i]]=fullwords[i]

# apply dictionary to test 
res=" ".join(d.get(s,s) for s in test.split()) 
# print test data after dictionary mapping
print(res) 

你的问题文本似乎表明你在每个单词的开头寻找你的短词。那应该很容易:

matched_words=[如果有任何单词,则以完整单词逐字排列。开始时以shortwords表示shortwords] 如果您出于某种原因想将其正则化,那么它不太可能更快,您可以通过大量替换来实现:

regex_alternation='|'.joinre.escapeSortword表示短词中的短词 matched_words=[如果re.matchrf ^{regex_alternation},则以完整字逐字排列,word] 或者,如果您的短词始终为四个字符,您可以将前四个字符切掉:

shortwords=setshortwords集合有O1查找,因此这将保存 如果其中一个短词 或者长词很长 匹配的单词=[如果单词[:4]为短词,则为完整单词中的单词]
这个代码段具有我想要的功能。它在每个循环迭代中构建一个正则表达式模式,以适应不同的字长参数。此外,它还保持wordroots列表的原始顺序。本质上,它查看wordroot中的每个单词,并从数据集中填写完整的单词。这在使用单词列表时非常有用,该列表包含长度为3-8个字符的单词,并且可以通过其首4个字符进行唯一识别。恢复短语是通过从bip-0039列表中随机选择一系列单词来构建的,顺序很重要。观察到的安全实践通常是将每个单词缩写为最多四个首字母。以下代码将根据其缩写重建恢复短语:

import re
wordroots = ['sun', 'sunk', 'sunn', 'suns']
dataset = ['sun', 'sunk', 'sunny', 'sunshine']
retrieved = []
for root in wordroots:
    #(exact match) or ((exact match at beginning of word when root is 4 or more characters) else (exact match))
    pattern = r"(^" + root + "$|" + ("^" + root + "[a-zA-Z]+)" if len(root) >= 4 else "^" + root + "$)")
    retrieved.extend( filter(lambda i: re.match(pattern, i), dataset) )
print(*retrieved)
输出:

sun sunk sunny sunshine

这并不能给出正确的结果。你的理解正确吗?我认为结果是正确的。只需要匹配完整单词开头字符上的短单词。还需要在检索到的列表中保留短词列表的顺序。您的示例适用于提供的数据片段,但在我测试应用程序中的完整数据集时不起作用:-希望能有更像蟒蛇的东西,并且能够控制比赛是由全字的前四个字符决定的-我确实忽略了四个字符的限制。这实际上使它更容易。去查字典。有趣的解决方案,谢谢。当我尝试这些方法时,我注意到我想保留短词列表中的顺序,并且这些解决方案按照完整词列表的顺序产生结果。@GoFaster这并不难,但不清楚您到底想要什么。这也可能会降低解决方案的性能。您可以尝试类似于result={shortword:[fullword for fullwords if fullwords.startswithshortword]for shortwords}的方法,这将为您提供一个短词:[fullwords]对字典。这有帮助吗?事实上,您可以将它们与list*result.values串在一起