473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dict Comprehension ?

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
Oct 6 '08 #1
6 2718
Ernst-Ludwig Brust:
Given 2 Number-Lists say l0 and l1,
count the various positiv differences between the 2 lists
...
i wonder, if there is a way, to avoid the list dif
Instead of creating the list (array) dif, you can create a lazy
iterator. Then you can fed it to a set.

Bye,
bearophile
Oct 6 '08 #2
On Oct 6, 8:59 am, "Ernst-Ludwig Brust" <seinnix...@online.dewrote:
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
Oct 6 '08 #3
"Ernst-Ludwig Brust" <se********@online.dewrites:
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
You can iterate over any iterable. A generator is iterable; and you
can (among other ways) create a generator with a generator
expression.

Fortunately, you already know how to write any generator expression:
it's the same as a list comprehension, but without the square
brackets.

So, instead of making a list of differences and iterating over it::
>>seq_a = [15, 17, 26]
seq_b = [14, 17, 22]
diffs = [abs(a - b) for a in seq_a for b in seq_b]
diff_accumulator = {}
for diff in diffs:
... diff_accumulator[diff] = diff_accumulator.get(diff, 0) + 1
...
>>diff_accumulator
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 7: 1, 9: 1, 12: 1}

you can skip the intermediate list creation and iterate over the
generator made by an identical generator expression::
>>seq_a = [15, 17, 26]
seq_b = [14, 17, 22]
diff_accumulator = {}
for diff in (abs(a - b) for a in seq_a for b in seq_b):
... diff_accumulator[diff] = diff_accumulator.get(diff, 0) + 1
...
>>diff_accumulator
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 7: 1, 9: 1, 12: 1}

If you wanted to avoid starting the dict empty and writing the ‘for’
loop yourself, you could even create your dict from a generator (the
dict type can make a new dict from any key+value iterable) with the
help of the standard library ‘itertools’ module::
>>import itertools
seq_a = [15, 17, 26]
seq_b = [14, 17, 22]
diffs = [abs(a - b) for a in seq_a for b in seq_b]
diff_accumulator = dict(
... (diff, len(list(items)))
... for (diff, items) in itertools.groupby(diffs)
... )
>>diff_accumulator
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 7: 1, 9: 1, 12: 1}

This also, of course, benefits from the earlier approach to iterate
over the diffs directly instead of an intermediate list::
>>import itertools
seq_a = [15, 17, 26]
seq_b = [14, 17, 22]
diff_accumulator = dict(
... (diff, len(list(items)))
... for (diff, items) in itertools.groupby(
... abs(a - b) for a in seq_a for b in seq_b
... )
... )
>>diff_accumulator
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 7: 1, 9: 1, 12: 1}

So, generator expressions can be a powerful way to clarify the purpose
of a section of code. They can be over-used, though: don't use them
unless they actually *do* clarify the code; sometimes an explicit
looping construct is clearer.

--
\ “Like the creators of sitcoms or junk food or package tours, |
`\ Java's designers were consciously designing a product for |
_o__) people not as smart as them.” —Paul Graham |
Ben Finney
Oct 7 '08 #4
<be************@lycos.comschrieb im Newsbeitrag
news:5b**********************************@m74g2000 hsh.googlegroups.com...
>
Instead of creating the list (array) dif, you can create a lazy
iterator. Then you can fed it to a set.
Thangs,
this idea is not only less storage-consuming but even faster
than the "List-Version".
But, i used the idea of pruebauno,
as this is faster.

Ernst-Ludwig Brust
Oct 7 '08 #5
"Ben Finney" <bi****************@benfinney.id.auschrieb im Newsbeitrag
news:87************@benfinney.id.au...
"Ernst-Ludwig Brust" <se********@online.dewrites:

So, generator expressions can be a powerful way to clarify the purpose
of a section of code. They can be over-used, though: don't use them
unless they actually *do* clarify the code; sometimes an explicit
looping construct is clearer.
Thangs,
my opinion was, that "one-statement-constructions" are
much faster then f.e. explicit loops.
But,
now i know, with Python, this is not allways true.

Ernst-Ludwig Brust
Oct 7 '08 #6
<pr*******@latinmail.comschrieb im Newsbeitrag
news:7c**********************************@75g2000h so.googlegroups.com...
>
from collections import defaultdict
da=defaultdict(int)
for x in [10]:
for y in [11]:
da[abs(x-y)]+=1
Thangs,
collections are a real good idea.
I will use this version.

Ernsst-Ludwwig Brust
Oct 7 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Xavier | last post by:
Greetings, (do excuse the possibly comical subject text) I need advice on how I can convert a text db into a dict. Here is an example of what I need done. some example data lines in the...
9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after...
7
by: Marcio Rosa da Silva | last post by:
Hi! In dictionaries, unlinke lists, it doesn't matter the order one inserts the contents, elements are stored using its own rules. Ex: >>> d = {3: 4, 1: 2} >>> d {1: 2, 3: 4}
7
by: George Young | last post by:
I am puzzled that creating large dicts with an explicit iterable of key,value pairs seems to be slow. I thought to save time by doing: palettes = dict((w,set(w)) for w in words) instead of: ...
2
by: Gerardo Herzig | last post by:
Hi all: I have this list thing as a result of a db.query: (short version) result = and so on...what i need to do is some list comprehension that returns me something like result = }, {...
4
by: bearophileHUGS | last post by:
I have started doing practice creating C extensions for CPython, so here are two ideas I have had, possibly useless. If you keep adding elements to a CPython dict/set, it periodically rebuilds...
14
by: Ben | last post by:
I have recently learned how list comprehension works and am finding it extremely cool. I am worried, however, that I may be stuffing it into places that it does not belong. What's the most...
14
by: erikcw | last post by:
Hi, I'm trying to turn o list of objects into a dictionary using a list comprehension. Something like entries = {} = d.id] for d in links]
22
by: mrkafk | last post by:
Hello everyone, I have written this small utility function for transforming legacy file to Python dict: def lookupdmo(domain): lines = open('/etc/virtual/domainowners','r').readlines()...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.