Dict Comprehension ?

pruebauno at latinmail.com pruebauno at latinmail.com
Mon Oct 6 10:27:46 EDT 2008


On Oct 6, 8:59 am, "Ernst-Ludwig Brust" <seinnix... at online.de> wrote:
> Given 2 Number-Lists say l0 and l1,
> count the various positiv differences between the 2 lists
>
> the following part works:
>
> dif=[abs(x-y) for x in l0 for y in l1]
> da={}
> for d in dif: da[d]=da.get(d,0)+1
>
> i wonder, if there is a way, to avoid the list dif
>
> Ernst-Ludwig Brust

from collections import defaultdict
da=defaultdict(int)
for x in [10]:
    for y in [11]:
        da[abs(x-y)]+=1



More information about the Python-list mailing list