easy but difficult

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Oct 16 12:09:45 EDT 2007


Paul McGuire a écrit :
> On Oct 16, 5:46 am, "Amit Khemka" <khemkaa... at gmail.com> wrote:
> 
(snip)
> Another technique that helps improve script readability is to assign
> the results of the split into separate variables, instead of just a
> list.  This way you can refer to the items with meaningful names
> instead of less readable index-into-the-list references:
> 
> for line in open('abc_file'):
>    datakey,datavalue = line.strip().split('#')
>    # add the numbers to the 'alphabet' key as a list
>    d[datakey] = d.get(datakey, []) + [datavalue]

And then use dict.setdefault and list.append:
   d.setdefault(datakey, []).append(datavalue)





More information about the Python-list mailing list