简体   繁体   中英

Separating compound nouns from basic nouns

I have a list that goes like this:

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

Is there a code that separate the compound nouns from the basic nouns? So that I can get:

name = ['road', 'ball']

Thanks.

All words that do not include any other words as a substring:

>>> [x for x in name if not any(word in x for word in name if word != x)]
    ['road', 'ball']

One way to print names using loops:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM