我在python中有一个或多个列表,类似于以下内容:
[
['name1',value2],
['name2',value3],
['name3',value4],
['name4',value4],
['name5',value5],
['name6',value2],
['name7',value2],
['name8',value4]
]
我想从“值”字段中删除列表中任何包含2个以上重复项的列表。结果列表如下所示:
[
['name1',value2],
['name2',value3],
['name3',value4],
['name4',value4],
['name5',value5],
['name6',value2]
]
修改
我不认为这会是一个问题,所以保持简单明确的问题,但我实际上有四个值,而不是每个内部列表中的两个。即:
[
['name1',value2,'something','else'],
['name2',value3,'something','else'],
['name3',value4,'something','else'],
['name4',value4,'something','else'],
['name5',value5,'something','else'],
['name6',value2,'something','else']
]
Ashwini Chaudhary的答案完美无缺,但只返回了第一个元素而不是全部四个......我没有添加完整的细节。经验教训!
答案 0 :(得分:2)
这段代码可以解决问题:
from collections import defaultdict
def dup2(sequence):
seen = defaultdict(int)
for key, value in sequence:
if seen[value] < 2:
seen[value] += 1
yield [key, value]
dup2
是一个生成器,因此当您迭代结果时它会处理列表:
for key, value in dup2(seq):
# ... your code here
要将结果作为普通列表,请使用list
函数:
list(dup2(seq))
答案 1 :(得分:1)
如果订单无关紧要:
In [14]: lis=[
['name1','value2','something','else'],
['name2','value3','something','else'],
['name3','value4','something','else'],
['name4','value4','something','else'],
['name5','value5','something','else'],
['name6','value2','something','else']
]
In [22]: dic={}
In [23]: for x in lis:
dic.setdefault(x[1],[]).append([x[0]]+x[2:])
....:
....:
In [25]: dic
Out[25]:
{'value2': [['name1', 'something', 'else'], ['name6', 'something', 'else']],
'value3': [['name2', 'something', 'else']],
'value4': [['name3', 'something', 'else'], ['name4', 'something', 'else']],
'value5': [['name5', 'something', 'else']]}
In [27]: [[y[0]]+[x]+y[1:] for x in dic for y in dic[x][:2]]
Out[27]:
[['name5', 'value5', 'something', 'else'],
['name3', 'value4', 'something', 'else'],
['name4', 'value4', 'something', 'else'],
['name2', 'value3', 'something', 'else'],
['name1', 'value2', 'something', 'else'],
['name6', 'value2', 'something', 'else']]
答案 2 :(得分:0)
from collections import defaultdict
list1 = [['name1','value2'],
['name2','value3'],
['name3','value4'],
['name4','value4'],
['name5','value5'],
['name6','value2'],
['name7','value2'],
['name8','value4']]
list2 = [['name1','value2'],
['name2','value3'],
['name3','value4'],
['name4','value4'],
['name5','value5'],
['name6','value2']]
d = defaultdict(list)
for name, value in list1:
d[value].append(name)
list3 = [[name, value] for value, names in d.items() for name in names[:2]]
print(sorted(list3) == sorted(list2)) # True
我确信有人会提出一个更好的解决方案来保留秩序并作为迭代器工作。