2014-09-01 233 views
0

列表中的其他元素的子列表中的所有元素,我有以下列表:删除在蟒蛇

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

正如你可以看到,JohnMauriceSebastianGeorge是名字或姓氏的全名(Maurice Smith,Jogn SebastianGeorge Washington)。

我想只得到全名。这在Python中可能吗?

+0

你已经试过了什么? – wRAR 2014-09-01 04:44:10

回答

3

你可以用这个列表理解其删除:

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

这遍历每个人p,然后检查条件:

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

在每个人p2这个内循环迭代(跳过情况与p相同),并检查p in p2(是否p是子字符串)。

+0

这将对整个列表中的每个项目执行完整搜索,并且如果其中一个长名称恰好在另一个中,则会形成奇怪的怪癖。 – kindall 2014-09-01 04:50:26

+0

@ kindall a)是的。有没有更快的算法可用(比n^2更快)? b)这恰好符合OP的要求(如标题所述)。如果一个长名称包含在另一个名称中,它将删除前者。 – 2014-09-01 04:53:17

+0

@kindall当然,算法可以存储每个元素的每个可能子字符串的字典。我怀疑这会提高大多数实际列表长度的性能,并且非常怀疑这会增加复杂性。 – 2014-09-01 04:56:49

0
# 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)