Skip to content Skip to sidebar Skip to footer

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 t

Solution 1:

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)ifword!=candidate and word in candidate:break# a compound. break inner loop, continue outerelse:# no breaks occured, must be a basic nounprintcandidate

Solution 2:

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

Post a Comment for "Separating Compound Nouns From Basic Nouns"