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 12 1364
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
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
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.
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
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
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
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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Fredrik Lundh |
last post by:
Patrol Sun wrote:
so why exactly are you trying to nest 20 or 100 for-in loops?
</F>
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
| |