删除python

时间:2017-12-22 23:39:16

标签: python dictionary duplicates dictionary-comprehension

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

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

考虑字典:

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

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

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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}

答案 2 :(得分: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])