Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
removing redundant elements in a dictionary
- From: stanc at al.com.au (Astan Chee)
- Subject: removing redundant elements in a dictionary
- Date: Wed, 01 Aug 2007 13:50:03 +1000
Hi,
I have a dictionary which looks something like this:
elem =
{"co":[1,2,3],"cp":[3,2,1],"pp":[5,6,7],"cl":[1,2,3],"qw":[6,7,8],"qa":[8,7,6]}
what Im trying to do is find all keys in the list that have the same
value and delete those (except one); thereby removing all redundant keys
so that the above dict will look like this:
elem = {"co":[1,2,3],"pp":[5,6,7],"qa":[8,7,6]}
my code so far looks like this:
for out1 in elem.keys():
for out2 in elem.keys():
if out1 != out2:
elem[out1].sort()
elem[out2].sort()
if elem[out1] == elem[out2]:
del elem[out1]
This returns in a KeyError exception when sorting. What am I missing here?
Thanks
Astan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-list/attachments/20070801/fe0f56d6/attachment.html
- Follow-Ups:
- removing redundant elements in a dictionary
- From: Antti Rasinen
- removing redundant elements in a dictionary
- Prev by Date: Any way to monitor windows network connection?
- Next by Date: Python end of file marker similar to perl's __END__
- Previous by thread: Any way to monitor windows network connection?
- Next by thread: removing redundant elements in a dictionary
- Index(es):