[BangPypers] list problem

steve steve at lonetwin.net
Thu Jul 22 12:42:04 CEST 2010


On 07/22/2010 03:21 PM, Vikram wrote:
> 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}
>
I don't get it, what's the criteria for creating xdictstart and xdictend ? Is 
start the lowest value of element 1 for each element 0 and xdictend the highest ?

If it is:

 >>> x =[['cat',10],['cat',20],['cat',30],['dog',5],['dog',1],['dog',3]]
 >>> d = {}
 >>> for k, v in x:
...     d.setdefault(k, []).append(v)
...
 >>> d
{'dog': [5, 1, 3], 'cat': [10, 20, 30]}
 >>> xstartdict = {}
 >>> xenddict = {}
 >>> for k, v in d.items():
...     xstartdict[k] = min(v)
...     xenddict[k] = max(v)
...
 >>> xstartdict, xenddict
({'dog': 1, 'cat': 10}, {'dog': 5, 'cat': 30})
 >>>

cheers,
- steve

-- 
random spiel: http://lonetwin.net/
what i'm stumbling into: http://lonetwin.stumbleupon.com/


More information about the BangPypers mailing list