问题
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: '')
I'm looking for the output to be
# newDict = {'key1': '', 'key2':['item4', 'item5', 'item6'], 'key3': '', 'key4':
'item8', key5: ['item1', 'item2', 'item3'], 'key6': 'item7'}
回答1:
You have the right overall idea, but there are three problems with your code:
- 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
.
Here is the correct code:
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
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
If you need duplicate values removed in a specific order, check out OrderedDicts.
Update:
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'}
with this:
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.
回答2:
This does it:
myDict_values = myDict.values() # better than calling this numerous times
for key in myDict.keys():
if myDict_values.count(myDict[key]) > 1: myDict[key] = ""
This won't guarantee that key5
will be blank instead of key1
, because dictionaries are not ordered.
来源:https://stackoverflow.com/questions/18255068/remove-duplicate-value-from-dictionary-without-removing-key