删除python中字典值的重复项

问题描述 投票:4回答:3

对不起主题的标题含糊不清,我觉得很难解释。

我有一个字典,其中每个值都是一个项目列表。我希望删除重复的项目,以便每个项目在列表中显示最少时间(最好是一次)。

考虑字典:

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]}

我可以将问题放到第一部分并进行管理,但我更喜欢将这两个部分放在一起解决

python dictionary duplicates dictionary-comprehension
3个回答
1
投票
#!/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)

0
投票

这可能不是最有效的解决方案。因为它涉及对所有可能组合的迭代,所以对于大型目标它将运行得非常慢。

它利用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}

0
投票

这可能有所帮助

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])
© www.soinside.com 2019 - 2024. All rights reserved.