473,405 Members | 2,154 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,405 software developers and data experts.

Coding Nested Loops

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
12 1433
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
4
by: dw | last post by:
Hello all. We're doing a site with teams and their members. We've got a page where we need to display people according to who belongs to a which team. I've heard that nested loops are bad, but...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.