删除python中字典值的重复项
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了删除python中字典值的重复项相关的知识,希望对你有一定的参考价值。
对不起主题的标题含糊不清,我觉得很难解释。
我有一个字典,其中每个值都是一个项目列表。我希望删除重复的项目,以便每个项目在列表中显示最少时间(最好是一次)。
考虑字典:
example_dictionary = {"weapon1":[1,2,3],"weapon2":[2,3],"weapon3":[2,3]}
'weapon2'和'weapon3'具有相同的值,因此它应该导致:
result_dictionary = {"weapon1":[1],"weapon2":[3],"weapon3":[2]}
因为我不介意订单,它也可能导致:
result_dictionary = {"weapon1":[1],"weapon2":[2],"weapon3":[3]}
但是当“没有选择”时,它应该留下价值。考虑这个新词典:
example_dictionary = {"weapon1":[1,2,3],"weapon2":[2,3],"weapon3":[2,3],"weapon4":[3]}
现在,因为它只能在不将键留空的情况下分配“2”或“3”,所以可能的输出是:
result_dictionary = {"weapon1":[1],"weapon2":[3],"weapon3":[2],"weapon4":[3]}
我可以将问题放到第一部分并进行管理,但我更喜欢将这两个部分放在一起解决
答案
#!/usr/bin/env python3
example_dictionary = {"weapon1":[1,2,3],"weapon2":[2,3],"weapon3":[2,3]}
result = {}
used_values = []
def extract_semi_unique_value(my_list):
for val in my_list:
if val not in used_values:
used_values.append(val)
return val
return my_list[0]
for key, value in example_dictionary.items():
semi_unique_value = extract_semi_unique_value(value)
result[key] = [semi_unique_value]
print(result)
另一答案
这可能不是最有效的解决方案。因为它涉及对所有可能组合的迭代,所以对于大型目标它将运行得非常慢。
它利用itertools.product()
来获得所有可能的组合。然后在其中,尝试找到具有最独特数字的组合(通过测试集合的长度)。
from itertools import product
def dedup(weapons):
# get the keys and values ordered so we can join them back
# up again at the end
keys, vals = zip(*weapons.items())
# because sets remove all duplicates, whichever combo has
# the longest set is the most unique
best = max(product(*vals), key=lambda combo: len(set(combo)))
# combine the keys and whatever we found was the best combo
return {k: [v] for k, v in zip(keys, best)}
从示例:
dedup({"weapon1":[1,2,3],"weapon2":[2,3],"weapon3":[2,3]})
#: {'weapon1': 1, 'weapon2': 2, 'weapon3': 3}
dedup({"weapon1":[1,2,3],"weapon2":[2,3],"weapon3":[2,3],"weapon4":[3]})
#: {'weapon1': 1, 'weapon2': 2, 'weapon3': 2, 'weapon4': 3}
另一答案
这可能有所帮助
import itertools
res = {'weapon1': [1, 2, 3], 'weapon2': [2, 3], 'weapon3': [2, 3]}
r = [[x] for x in list(set(list(itertools.chain.from_iterable(res.values()))))]
r2 = [x for x in res.keys()]
r3 = list(itertools.product(r2,r))
r4 = dict([r3[x] for x in range(0,len(r3)) if not x%4])
以上是关于删除python中字典值的重复项的主要内容,如果未能解决你的问题,请参考以下文章
python学习--根据字典中的值的大小,对字典的项进行排序。
如何在不删除先前相同值的情况下选择具有重复项的列表中的特定数字?