简体   繁体   中英

Remove all elements of a list that are substrings of other elements of the list in python

I have the following list:

people = ['John', 'Maurice Smith', 'Sebastian', 'Maurice', 'John Sebastian', 'George', 'George Washington']

As you can notice, John , Maurice , Sebastian and George are names or last names of the full names ( Maurice Smith , Jogn Sebastian and George Washington ).

I would like to get only the full names. Is this possible in python?

You can remove them with this list comprehension:

[p for p in people if not any(p in p2 for p2 in people if p != p2)]

This iterates over each person p , then checks the condition:

not any(p in p2 for p2 in people if p != p2)

This inner loop iterates over each person p2 (skipping the case where it is the same as p ), and checks p in p2 (whether p is a substring).

# make set of first names from full names
firstnames = set(name.split[0] for name in people if " " in name)

# get names that aren't in the above set
people[:] = (name for name in people if name not in firstnames)

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