1

我有一个这样的列表:

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

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

name = ['road', 'ball']

谢谢。

4

2 回答 2

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 
于 2013-03-25T13:54:41.387 回答
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])]
于 2013-03-25T14:01:19.143 回答