dictionary of list from a file
keirr
keir.robinson at gmail.com
Wed Oct 4 09:34:53 EDT 2006
andrea.spitaleri at gmail.com wrote:
<snip perl example code>
> in python I tried:
> b={}
> a=[]
> for line in fl.readlines():
> info=lines.split()
> b[info[0]] = a.append(info[1])
>
> and then
> for i in b:
> print i,b[i]
> i get
> 2 None
> 7 None
>
> data file is:
> 2 1
> 2 2
> 2 3
> 2 4
> 7 7
> 7 8
> 7 9
> 7 10
>
> Any help??
Andrea,
first the append method returns None, as you have discovered; it makes
an inplace update (to a in your example) but does not return the list
it has updated. This has surprised a few people in the past :-)
Here is what I've used before for updating lists values in a
dictionary.
d[key] = d.get(key, []) + [value]
where d is a dict(ionary)
Hope this helps.
Keir.
More information about the Python-list
mailing list