[Baypiggies] list problem
Vikram
kpguy at rediffmail.com
Thu Jul 22 11:53:46 CEST 2010
Suppose you have the following list:
>>> x =[['cat',10],['cat',20],['cat',30],['dog',5],['dog',1],['dog',3]]
My problem is that i wish to obtain the following two dictionaries:
xdictstart = {'cat':10, 'dog':1}
xdictend = {'cat':30, 'dog':5}
Any nice way to do the above? Thanks.
-------
Those interested in the above problem may consider the following code (which does not actually do what i want):
>>> xdictend = dict(x)
>>> xdictend
{'dog': 3, 'cat': 30}
>>> x
[['cat', 10], ['cat', 20], ['cat', 30], ['dog', 5], ['dog', 1], ['dog', 3]]
>>> xdictstart = {}
>>> map(xdictstart.setdefault, *zip(*x))
[10, 10, 10, 5, 5, 5]
>>> xdictstart
{'dog': 5, 'cat': 10}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20100722/e85b58bc/attachment.html>
More information about the Baypiggies
mailing list