【问题标题】:Removing duplicates in values of dictionary in python在python中删除字典值中的重复项
【发布时间】:2018-06-05 12:30:55
【问题描述】:

抱歉题目含糊不清,我很难解释。

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

考虑字典:

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

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

【问题讨论】:

  • 你能解释一下为什么 Weapon1 在结果中有 [1] 吗?武器 2 和武器 3 将从第一个武器 1 [1,2,3] 中获取值?
  • @AyodhyankitPaul weapon1 是唯一一个在他的列表中有 [1] 的人,这就是它有 [1] 的原因。武器 2 和武器 3 都有 [2,3],所以每个都有一个单独的数字,其中一个得到 [2],另一个得到 [3]

标签: python dictionary duplicates dictionary-comprehension


【解决方案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)

【讨论】:

  • 这不适用于{'weaponA': [1, 2], 'weaponB': [1]} 之类的情况。
  • 总唯一性不是一个约束。如果我正确理解了这个问题,这种情况将给出 [1] 和 [1] 这是可以接受的。
  • 如果可能的话,我宁愿得到 [1] 和 [2],因为输入的大小可能很大
【解决方案2】:

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

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

【讨论】:

    【解决方案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])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 2017-01-22
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 2010-12-21
      • 2016-03-13
      相关资源
      最近更新 更多