Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于特定键和值从列表中删除重复字典_Python_Dictionary - Fatal编程技术网

Python 基于特定键和值从列表中删除重复字典

Python 基于特定键和值从列表中删除重复字典,python,dictionary,Python,Dictionary,这是我当前的词典列表: list1 = [{"one" : 1, "too": 2, "three": 3}, {"one" : 10, "too": 20, "three": 30} , {"one" : 1, "too": 26, "three": 35}, {"one" : 1, "too": 236, "three": 365}] 我想从基于键1的列表中删除所有重复项。 这意味着在键1中,该列表上另一个字典的值相同,请将其删除。 我希望输出为: list1 = [{"one" : 1,

这是我当前的词典列表:

list1 = [{"one" : 1, "too": 2, "three": 3}, {"one" : 10, "too": 20, "three": 30} , {"one" : 1, "too": 26, "three": 35}, {"one" : 1, "too": 236, "three": 365}]
我想从基于键1的列表中删除所有重复项。 这意味着在键1中,该列表上另一个字典的值相同,请将其删除。 我希望输出为:

list1 = [{"one" : 1, "too": 2, "three": 3}, {"one" : 10, "too": 20, "three": 30}]
应该为键1保留一组可见值。然后,利用or运算符的工作原理,可以使用简单的循环或复杂的理解,如以下所示:

list1 = [{"one" : 1, "too": 2, "three": 3}, {"one" : 10, "too": 20, "three": 30} , {"one" : 1, "too": 26, "three": 35}, {"one" : 1, "too": 236, "three": 365}]

seen = set()
list1[:] = [seen.add(d["one"]) or d for d in list1 if d["one"] not in seen]
# [{'one': 1, 'too': 2, 'three': 3}, {'one': 10, 'too': 20, 'three': 30}]
切片分配只是确保这是原始列表上的一个变异,例如,如果要使用它修改函数参数。 在任何情况下,您都应该从头开始构建列表,因为从列表中重复删除是一种性能很差的反模式。

您应该为键1保留一组可见值。然后,利用or运算符的工作原理,可以使用简单的循环或复杂的理解,如以下所示:

list1 = [{"one" : 1, "too": 2, "three": 3}, {"one" : 10, "too": 20, "three": 30} , {"one" : 1, "too": 26, "three": 35}, {"one" : 1, "too": 236, "three": 365}]

seen = set()
list1[:] = [seen.add(d["one"]) or d for d in list1 if d["one"] not in seen]
# [{'one': 1, 'too': 2, 'three': 3}, {'one': 10, 'too': 20, 'three': 30}]
切片分配只是确保这是原始列表上的一个变异,例如,如果要使用它修改函数参数。 在任何情况下,您都应该从头开始构建列表,因为从列表中重复删除是一种性能很差的反模式。

尝试:

list1 = [{"one" : 1, "too": 2, "three": 3}, {"one" : 10, "too": 20, "three": 30} , {"one" : 1, "too": 26, "three": 35}, {"one" : 1, "too": 236, "three": 365}]

l = [d for index,d in enumerate(list1) if d["one"] not in set(map(lambda x:x["one"],list1[:index]))]
结果:

[{'one': 1, 'too': 2, 'three': 3}, {'one': 10, 'too': 20, 'three': 30}]
Unique List ->  [{'one': 1, 'too': 2, 'three': 3}, {'one': 10, 'too': 20, 'three': 30}]
Unique Keys ->  [1, 10]
尝试:

结果:

[{'one': 1, 'too': 2, 'three': 3}, {'one': 10, 'too': 20, 'three': 30}]
Unique List ->  [{'one': 1, 'too': 2, 'three': 3}, {'one': 10, 'too': 20, 'three': 30}]
Unique Keys ->  [1, 10]

这将为您提供唯一列表以及列表中的唯一键

代码:

结果:

[{'one': 1, 'too': 2, 'three': 3}, {'one': 10, 'too': 20, 'three': 30}]
Unique List ->  [{'one': 1, 'too': 2, 'three': 3}, {'one': 10, 'too': 20, 'three': 30}]
Unique Keys ->  [1, 10]

这将为您提供唯一列表以及列表中的唯一键

代码:

结果:

[{'one': 1, 'too': 2, 'three': 3}, {'one': 10, 'too': 20, 'three': 30}]
Unique List ->  [{'one': 1, 'too': 2, 'three': 3}, {'one': 10, 'too': 20, 'three': 30}]
Unique Keys ->  [1, 10]

只有当它不打算删除重复条目的所有副本,而是保留第一个看到的副本时,它才起作用。我想说,从OPs期望的输出来看,这是有意的。否则,您总是会得到一个空列表,不是吗?只有当它不是要删除重复条目的所有副本,而是要保留第一个看到的副本时,这才有效。我想说,从OPs期望的输出来看,这是有意的。否则你总是会得到一张空名单,不是吗?