[英]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.