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

 

ループを使用して名前を印刷する1つの方法:

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 に答える