[英]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
). 如您所见, John
, Maurice
, Sebastian
和George
是全名的名字或姓氏( Maurice Smith
, Jogn Sebastian
和George Washington
)。
I would like to get only the full names. 我只想知道全名。 Is this possible in python? 这可能在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: 这遍历每个人p
,然后检查条件:
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). 此内部循环遍历每个人p2
(跳过与p
相同的情况),并检查p in p2
( p
是否为子字符串)。
# 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.