从字典中删除重复值而不删除键 [英] Remove duplicate value from dictionary without removing key
问题描述
我是Python的新手,不得不通过试用和错误的学习,但是我无法找到我遇到的问题的解决方案。
I'm new to Python and am having to learn by trial and error, but I haven't been able to find a solution to the problem I'm having.
我有一本类似这样的字典:
I have a dictionary that looks something like this:
myDict = {'key1': ['item1', 'item2', 'item3'], 'key2': ['item4', 'item5', 'item6'],
'key3': 'item7', 'key4': 'item8', 'key5': ['item1', 'item2', 'item3'], 'key6': 'item7'}
我需要从字典中删除重复的值,并用空值()替换它们。在这里找到了一对夫妇的解决方案,但是他们正在按照预期的方式工作。
I need to remove duplicate values from the dictionary and replace them with an empty value (""). Found a couple solution on here but they are working as intended
for key, value in myDict.items():
if values not in key newDict.values():
myDict[key] = value
else:
myDict[key] = ""
print newDict
这是删除所有的值,并输出
This is removing all the values and is outputting
# newDict:{key1: '', key2: '', key3: '', key4: '', key5: '', key6: '')
我正在寻找输出为
# newDict = {'key1': '', 'key2':['item4', 'item5', 'item6'], 'key3': '', 'key4':
'item8', key5: ['item1', 'item2', 'item3'], 'key6': 'item7'}
推荐答案
你有正确的总体想法,但你的代码有三个问题:
You have the right overall idea, but there are three problems with your code:
- 您正在将值存储回
myDict
而不是newDict
。 - 在第2行,你正在检查
值
而不是值
。 - 同样在第2行,
键
不应该在那里,并抛出一个SyntaxError
。
- You're storing values back into
myDict
instead of intonewDict
. - On line 2, you're checking
values
instead ofvalue
. - Also on line 2,
key
shouldn't be there, and throws aSyntaxError
.
这是正确的代码:
newDict = {}
for key, value in myDict.iteritems():
if value not in newDict.values():
newDict[key] = value
else:
newDict[key] = ""
print newDict
如果您不在反三位运营商营地,您还可以将其缩短为:
If you're not in the anti-ternary operator camp, you could also shorten it to this:
newDict = {}
for key, value in myDict.iteritems():
newDict[key] = value if value not in newDict.values() else ""
print newDict
或者,如果您宁愿从原始 dict
( myDict
)中删除值,而不是构建一个ne w $($ code> newDict ),你可以这样做:
Or, if you would rather just remove the values from the original dict
(myDict
) instead of building a new one (newDict
), you could do this:
foundValues = []
for key, value in myDict.iteritems():
if value not in foundValues:
foundValues.append(myDict[key])
else:
myDict[key] = ""
print myDict
如果您需要在具体订单,请查看 OrderedDict
s 。
If you need duplicate values removed in a specific order, check out OrderedDict
s.
更新:
更新的要求 - 从最初的 dict
中删除该值,如果您能够简单地初始化 myDict
与 OrderedDict
而不是 dict
,所有您需要做的是替换为: p>
In light of the updated requirements -- that values be removed from the original dict
, starting from the beginning -- if you're able to simply initialize myDict
with an OrderedDict
instead of a dict
, all you need to do is replace this:
myDict = {'key1': ['item1', 'item2', 'item3'], 'key2': ['item4', 'item5', 'item6'], 'key3': 'item7', 'key4': 'item8', 'key5': ['item1', 'item2', 'item3'], 'key6': 'item7'}
与此相关:
from collections import OrderedDict
…
myDict = OrderedDict([('key1', ['item1', 'item2', 'item3']), ('key2', ['item4', 'item5', 'item6']), ('key3', 'item7'), ('key4', 'item8'), ('key5', ['item1', 'item2', 'item3']), ('key6', 'item7')])
然后使用与上述相同的代码。
and then use the same code provided above.
这篇关于从字典中删除重复值而不删除键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!