By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

Coding Nested Loops

P: n/a
I want to code what would be nested "for" loops in C, but I don't know the
most elegant way of doing the same thing in python. So I need to learn how
from you folks. Here's what I need to do: build a database table of 180
rows. Each row contains 31 columns: the first is an automatically
incremented integer as the primary key; the next two fields can each contain
one of three strings held in dictionaries, the last 28 fields are random
floats from a third dictionary.

Of the 180 total rows, each of the three values in the first dictionary
will be the second field in 60 rows. Within each set of 60 rows, there will
be 13 rows containing the first value from the second dictionary, 14 rows
containing the second value from the second dictionary, and 33 rows from
the third value in that dictionary. Again, the final 28 fields in each row
are random values from the third dictionary.

I suspect that iterators might be the way to accomplish this, but I really
don't know. However, I would like to learn so that I can solve similar
problems by myself.

TIA,

Rich

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc.(TM) | Accelerator
<http://www.appl-ecosys.com Voice: 503-667-4517 Fax: 503-667-8863
Sep 15 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Rich Shepard wrote:
I want to code what would be nested "for" loops in C, but I don't know
the
most elegant way of doing the same thing in python. So I need to learn how
from you folks. Here's what I need to do: build a database table of 180
rows. Each row contains 31 columns: the first is an automatically
incremented integer as the primary key; the next two fields can each
contain one of three strings held in dictionaries, the last 28 fields are
random floats from a third dictionary.

Of the 180 total rows, each of the three values in the first dictionary
will be the second field in 60 rows. Within each set of 60 rows, there
will be 13 rows containing the first value from the second dictionary, 14
rows containing the second value from the second dictionary, and 33 rows
from the third value in that dictionary. Again, the final 28 fields in
each row are random values from the third dictionary.

I suspect that iterators might be the way to accomplish this, but I
really
don't know. However, I would like to learn so that I can solve similar
problems by myself.
It's not clear to me why you would use dictionaries, especially as they are
unordered; I used lists instead:

from itertools import count, izip, cycle, chain, repeat, starmap, imap
from random import choice

first = ["X", "Y", "Z"]
second = ["A", "B", "C"]
second_count = [13, 14, 33]
third = [1.1, 2.2, 3.3, 4.4]

random_floats = imap(choice, repeat(third))
columns = [
count(),
chain(*[repeat(i, 60) for i in first]),
cycle(chain(*starmap(repeat, izip(second, second_count))))
]
columns.extend(repeat(random_floats, 28))

for row in izip(*columns):
print row

Now that is a nice occasion to get acquainted with the itertools module...

Peter

Sep 15 '06 #2

P: n/a
On Fri, 15 Sep 2006, Peter Otten wrote:
It's not clear to me why you would use dictionaries, especially as they
are unordered; I used lists instead:
Peter,

Because the data comes via a serial port as sequences of two bytes from an
OMR reader, and the byte pairs need to be converted into values meaningful
to the application.

Rich

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc.(TM) | Accelerator
<http://www.appl-ecosys.com Voice: 503-667-4517 Fax: 503-667-8863
Sep 15 '06 #3

P: n/a
Rich Shepard wrote:
On Fri, 15 Sep 2006, Peter Otten wrote:
>It's not clear to me why you would use dictionaries, especially
as they are unordered; I used lists instead:
Because the data comes via a serial port as sequences of two
bytes from an
OMR reader, and the byte pairs need to be converted into values
meaningful to the application.
That doesn't answer the question. A list of 2-tuples would do the
same (and was ordered and could be indexed).

Regards,
Björn

--
BOFH excuse #408:

Computers under water due to SYN flooding.

Sep 15 '06 #4

P: n/a
On Fri, 15 Sep 2006, Bjoern Schliessmann wrote:
That doesn't answer the question. A list of 2-tuples would do the same
(and was ordered and could be indexed).
Björn, et al.:

For the purpose of generating a data sample, the list of 2-tuples will
work.

Thanks all,

Rich

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc.(TM) | Accelerator
<http://www.appl-ecosys.com Voice: 503-667-4517 Fax: 503-667-8863
Sep 15 '06 #5

P: n/a
Peter Otten wrote:
from itertools import count, izip, cycle, chain, repeat, starmap, imap
from random import choice

first = ["X", "Y", "Z"]
second = ["A", "B", "C"]
second_count = [13, 14, 33]
third = [1.1, 2.2, 3.3, 4.4]

random_floats = imap(choice, repeat(third))
columns = [
count(),
chain(*[repeat(i, 60) for i in first]),
cycle(chain(*starmap(repeat, izip(second, second_count))))
]
columns.extend(repeat(random_floats, 28))

for row in izip(*columns):
print row

Now that is a nice occasion to get acquainted with the itertools module...
Wow, that's the most comprehensive example of itertools (ab)use I have
seen! Awesome!

George

Sep 15 '06 #6

P: n/a
On Fri, 15 Sep 2006, Peter Otten wrote:
It's not clear to me why you would use dictionaries, especially as they
are unordered; I used lists instead:
...
Now that is a nice occasion to get acquainted with the itertools module...
Peter,

I have to study the docs to understand what's going on. But, I can see
that this would work.

Thank you,

Rich

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc.(TM) | Accelerator
<http://www.appl-ecosys.com Voice: 503-667-4517 Fax: 503-667-8863
Sep 15 '06 #7

P: n/a
Rich Shepard wrote:
On Fri, 15 Sep 2006, Peter Otten wrote:
>It's not clear to me why you would use dictionaries, especially as they
are unordered; I used lists instead:

...
>Now that is a nice occasion to get acquainted with the itertools
module...

Peter,

I have to study the docs to understand what's going on. But, I can see
that this would work.
As George hinted, I went a bit over the top with my itertools example. Here
is a translation into static lists (mostly):

from itertools import izip
from random import choice

first = ["X", "Y", "Z"]
second = ["A", "B", "C"]
third = [1.1, 2.2, 3.3, 4.4]

x, y, z = first
a, b, c = second

def random_floats(sample):
while 1:
yield choice(sample)

columns = [
range(180),
[x]*60 + [y]*60 + [z]*60,
([a]*13 + [b]*14 + [c]*33) * 3] + [random_floats(third)]*28

for row in izip(*columns):
print row

Of course nested loops will work, too. Use whatever you find easiest to
maintain.

Peter

Sep 16 '06 #8

P: n/a
On Sat, 16 Sep 2006, Peter Otten wrote:
As George hinted, I went a bit over the top with my itertools example.
Here is a translation into static lists (mostly):
Peter,

Thank you. This is clearer to me. While your original code certainly works
it reminded me of The C Users Journal's annual obfuscated C contests. :-)
from random import choice
Two questions germane to random: 1) Why wasn't choice available when I
used 'import random,' and 2) What are the differences between 'choice' and
'shuffle?'
Of course nested loops will work, too. Use whatever you find easiest to
maintain.
I found the library page and will continue looking for more comprehensive
documentation.

Rich

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc.(TM) | Accelerator
<http://www.appl-ecosys.com Voice: 503-667-4517 Fax: 503-667-8863
Sep 16 '06 #9

P: n/a
On Sat, 16 Sep 2006 08:29:26 -0700 (PDT),
Rich Shepard <rs******@appl-ecosys.comwrote:
Two questions germane to random: 1) Why wasn't choice available when
I used 'import random,' ...
When you import random, all you're doing is importing the module; you
have to specify any given attribute thereof:

import random
random.choice( [ 1, 5, 7 ] )
... 2) What are the differences between 'choice' and 'shuffle?'
choice chooses one element of a given list and leaves the list itself
alone; shuffle shuffles the list, presumably so that you can iterate
through the elements yourself.

HTH,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
Sep 16 '06 #10

P: n/a
On Sat, 16 Sep 2006, Dan Sommers wrote:
When you import random, all you're doing is importing the module; you have
to specify any given attribute thereof:
Dan,

I thought that was implied. For example, I use 'import wx' and can then
instantiate wx.frame, wx.dialogbox, etc. without explicitly importing each
one. Apparently different modules work differently.
>... 2) What are the differences between 'choice' and 'shuffle?'
choice chooses one element of a given list and leaves the list itself
alone; shuffle shuffles the list, presumably so that you can iterate
through the elements yourself.
OK. The former picks the element for you while with the latter we must
specify which element to pick.

Thank you. This is all very good to know.

Rich

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc.(TM) | Accelerator
<http://www.appl-ecosys.com Voice: 503-667-4517 Fax: 503-667-8863
Sep 16 '06 #11

P: n/a
On Sat, 16 Sep 2006 10:06:25 -0700 (PDT),
Rich Shepard <rs******@appl-ecosys.comwrote:
On Sat, 16 Sep 2006, Dan Sommers wrote:
>When you import random, all you're doing is importing the module; you
have to specify any given attribute thereof:
I thought that was implied. For example, I use 'import wx' and can
then instantiate wx.frame, wx.dialogbox, etc. without explicitly
importing each one. Apparently different modules work differently.
No, they all work the same way (thank goodness!). The "." between "wx"
and "frame" is the same dot as is between "random" and "choice" (i.e.,
random.choice is the same construct as wx.frame).

Don't be confused by from: if you use "from wx import frame," then you
can access frame *without* the "wx." bit. I don't have wx installed,
but that works with random, too:
>>choice
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'choice' is not defined
>>import random
random.choice
<bound method Random.choice of <random.Random object at 0x1841a10>>
>>from random import choice
choice
<bound method Random.choice of <random.Random object at 0x1841a10>>

HTH,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
Sep 16 '06 #12

P: n/a
On Sat, 16 Sep 2006, Dan Sommers wrote:
No, they all work the same way (thank goodness!). The "." between "wx"
and "frame" is the same dot as is between "random" and "choice" (i.e.,
random.choice is the same construct as wx.frame).
Ah, yes. I totally forgot this.

Thanks for the reminder,

Rich

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc.(TM) | Accelerator
<http://www.appl-ecosys.com Voice: 503-667-4517 Fax: 503-667-8863
Sep 16 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.