<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi guys, > this is my first post. my "programming" background is perlish scripting > and now I am learning python. I need to create a dictionary of list > from a file. Normally in perl I use to do like: > > while(<IN>){ > @info=split(/ +/,$_); > push (@{$tmp{$info[0]}},$info[1]); > } > > and then > foreach $key (keys %tmp){ > print "$key -> @{$tmp{$key}}\n"; > } > i get > > 2 -> 1 2 3 4 > 7 -> 7 8 9 10 > > 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?? > Thanks in advance > Best Regards > > Andrea >
I'll see your perlish line noise, and raise you this obfuscapython: :) data = """\ 2 1 2 3 4 7 7 8 9 10 5 1 3 5 7 9 2 6 8 10""".split('\n') # similar to file.readlines(), but easier to paste into news post import operator, itertools item = operator.itemgetter b = dict( (k,sum(map(lambda g:g[1:],grps),[])) for (k,grps) in itertools.groupby( sorted( map(str.split,data) ), item(0) ) ) for item in sorted(b.items()): print "%s -> %s" % item prints: 2 -> ['1', '2', '3', '4', '6', '8', '10'] 5 -> ['1', '3', '5', '7', '9'] 7 -> ['7', '8', '9', '10'] -- Paul -- http://mail.python.org/mailman/listinfo/python-list