对不起主题的标题含糊不清,我觉得很难解释.
我有一个字典,其中每个值都是一个项目列表.我希望删除重复的项目,以便每个项目在列表中显示最少时间(最好是一次).
考虑字典:
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)