takashi88
takashi88

Reputation: 37

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.

Upvotes: 1

Views: 300

Answers (2)

Pavel Anossov
Pavel Anossov

Reputation: 62928

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 

Upvotes: 5

Thorsten Kranz
Thorsten Kranz

Reputation: 12765

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

Upvotes: 0

Related Questions