我是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 . . .
我真的可以用您的幫助.. 。 非常感謝!
您的後綴和前綴是否始終位於單詞的開頭和結尾? – Yarick
當你運行del(...)你想從單詞數組中刪除?因爲現在它正在刪除整個圖案和motifp變量,這就是爲什麼崩潰。 – farbiondriven
是的,前綴始終在開頭,後綴始終在單詞的末尾。 –