![](/img/trans.png)
[英]How can I print a list of sets to a file, with python, in a format that is easily put into excel, eliminating all the unwanted characters?
[英]How can I remove unwanted characters from a words list and put them cleared in another list using Python?
我是Python的新手,正在研究词典数据库。 我有三个列表:第一个包含我要测试的数据库中的几个单词,第二个包含前缀,第三个包含后缀。 我需要制作另一个列表(称为“自由基”),该列表将包含第一个列表中与其他两个列表匹配的单词,但删除了它们的前缀或后缀。
我确定我在这里使用的方法不正确,但这是我的代码:
#coding UTF-8
import re
from re import search
words = ["flore", "fleur", "fleuriste", "remaniement", "remanier", "manier", "maniable", "désaimer", "aimer", "aimant", "mêler", "emmêler", "désemmêler"]
radicals = []
i = 0
motifp = "^[re|em|dés]"
motifs = "[iste|ment|er|ant]$"
while i < len(words) :
if re.search(motifs, words[i]) :
del(motifp, words[i])
del(motifs, words[i])
radicals.append(words[i])
i = i + 1
print(radicals)
它返回以下错误:
['fleur']
Traceback (most recent call last):
File "C:\Users\alice\OneDrive\Documents\Visual Studio 2017\Projects\PythonApplication4\PythonApplication4\PythonApplication4.py", line 14, in <module>
del(motifp, words[i])
NameError: name 'motifp' is not defined
Press any key to continue . . .
我真的可以帮助您...非常感谢!
您要遍历每个单词并删除所有定义的前缀或后缀。 而已。 并且由于某些部首是相同的,例如对于fleur和fleuriste,请使用set
。
import re
words = ["flore", "fleur", "fleuriste", "remaniement", "remanier", "manier", "maniable", "désaimer", "aimer", "aimant", "mêler", "emmêler", "désemmêler"]
radicals = set()
motifp = "^(re|em|dés)"
motifs = "(iste|ment|er|ant)$"
for word in words:
word = re.sub(motifp, '', word)
word = re.sub(motifs, '', word)
radicals.add(word)
print(radicals)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.