我试图使用缩写词列表来选择&;检索由其初始字符序列标识的对应完整单词:
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
所以正则表达式匹配是有效的,但问题是如何调整代码以迭代短单词列表并检索完整单词
编辑:解决方案需要保留短词列表中的顺序
也许用字典
你的问题文本似乎表明你在每个单词的开头寻找你的短词。那应该很容易:
如果出于某种原因(不太可能更快)希望对此进行正则化,则可以进行大量替换:
或者,如果您的短词是始终四个字符,您可以将前四个字符切掉:
这是一种方法:
这是一个经典的键值问题。使用字典,或长期考虑熊猫。
相关问题 更多 >
编程相关推荐