将复合名词与基本名词分开

时间:2013-03-25 13:47:29

标签: python

我有一个列表如下:

name = ['road', 'roadwork', 'roadblock', 'ball', 'football', 'basketball', 'volleyball']

是否有将复合名词与基本名词分开的代码?所以我可以得到:

name = ['road', 'ball']

感谢。

2 个答案:

答案 0 :(得分:5)

所有不包含任何其他单词作为子字符串的单词:

>>> [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 

答案 1 :(得分:0)

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])]