简体   繁体   English

如果有多个元素,请从列表中删除该元素的子集

[英]Remove subset of an element from a list if there are more than one

If I have a list like: 如果我有类似的清单:

[u'test_1', u'test_2', u'test_3', u'bananas_4', u'cake_5', u'bananas_6']

What would be the best way to just get the following without knowing anything else in advance? 在不事先知道其他任何信息的情况下,获得以下内容的最佳方法是什么?

[u'test_1', u'bananas_4', u'cake_5']

So how I see it, would be something like loop over the list, store the test and bananas somehow, and if on another iteration, see another of the same start of the string, remove that from the list. 所以我怎么看,就像在列表上循环一样,以某种方式存储testbananas ,如果在另一个迭代中,看到另一个与字符串相同的开头,则将其从列表中删除。

Does anyone know the best way of achieving this? 有谁知道实现这一目标的最佳方法?

My main idea uses the dictionary functionality that items are not overridden by default. 我的主要思想是使用字典功能,即默认情况下不覆盖项。

I used OrderedDict to keep the order of insertion of items. 我使用OrderedDict来保持项目插入的顺序。

lst = [u'test_1', u'test_2', u'test_3', u'bananas_4', u'cake_5', u'bananas_6']
d = OrderedDict()
for item in lst:
    key, val = item.split('_')
    d.setdefault(key, val) # will not override if item was there before

new_list = [key + '_' + val for key,val in d.items()]
print new_list

Output is 输出是

[u'test_1', u'bananas_4', u'cake_5']

Simply keep a set of your prefixes and only add items to your filtered list if they're not in the prefix list: 只需保留一组前缀,仅将项目添加到过滤列表中(如果它们不在前缀列表中):

start = [u'test_1', u'test_2', u'test_3', u'bananas_4', u'cake_5', u'bananas_6']

seen = set()
end = []

for item in start:
    prefix = item.partition('_')[0]
    if prefix not in seen:
        end.append(item)
        seen.add(prefix)

print(end)  # ['test_1', 'bananas_4', 'cake_5']

I would split it into two sections. 我将其分为两部分。 The first is to split the string in the list by "_" then you would have the raw information [test,test,test,banana,cake,banana] and another with the numbers [1,2,3,4,5,6] 第一种是将列表中的字符串除以“ _”,然后您将获得原始信息[test,test,test,banana,cake,banana],另一个具有数字[1,2,3,4,5, 6]

You could then find the uniques of the string list with the following solution: Get unique values from a list in python . 然后,您可以使用以下解决方案找到字符串列表的唯一性: 从python中的列表中获取唯一值 Finally append the numbers back on. 最后再加上数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM