[英]Separating compound nouns from basic nouns
我有一個這樣的清單:
name = ['road', 'roadwork', 'roadblock', 'ball', 'football', 'basketball', 'volleyball']
是否存在將復合名詞與基本名詞分開的代碼? 這樣我就可以得到:
name = ['road', 'ball']
謝謝。
不包含任何其他單詞作為子字符串的所有單詞:
>>> [x for x in name if not any(word in x for word in name if word != x)]
['road', 'ball']
使用循環打印名稱的一種方法:
for candidate in name:
for word in name:
# candidate is a compound if it contains any other word (not equal to it)
if word != candidate and word in candidate:
break # a compound. break inner loop, continue outer
else: # no breaks occured, must be a basic noun
print candidate
names = ['road', 'roadwork', 'roadblock', 'ball', 'football', 'basketball', 'volleyball']
basic_names = [name for name in names if not any([part for part in names if part in name and part != name])]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.