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 1461
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(*st armap(repeat, izip(second, second_count))) )
]
columns.extend( repeat(random_f loats, 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(*st armap(repeat, izip(second, second_count))) )
]
columns.extend( repeat(random_f loats, 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(s ample):
while 1:
yield choice(sample)
columns = [
range(180),
[x]*60 + [y]*60 + [z]*60,
([a]*13 + [b]*14 + [c]*33) * 3] + [random_floats(t hird)]*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******@app l-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.tombstoneze ro.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist 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 more complex, the number of required for-loops will
increase. Does Python have a limit on the number of nested for-loops?
Thanks.
|
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 what's the alternative? Would a
group-by clause in the SELECT do the trick? Right now we're doing:
(pseudo-code)---------------------------------
For each team
print team name
For each peson
print person name
|
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 already in a loop." Then he
goes on to portray a contrived example that doesn't tell me under what
conditions a nested loop might be favoured as a solution? i.e. what are
nested loops useful for? What kinds of algorithms are served by nested
loops?...
|
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 initiative.
Typically I find that coding standards are written by some guy in the
company who has a way of coding that he likes and then tries to force
everybody else to write code the way he likes it, not for any rational
reason, but simply for the...
|
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. Is this because .NET is inherently slower or does the C# compiler merely
produce code that is not as well optimized as the C++ compiler?
| |
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?
|
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 trying to understand Goto in
this contect.
PS: This is not homework.
|
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 ExpressionThen <My CodeElse Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next
If Not <Boolean ExpressionThen Exit For
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |