Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Python

for y in range (0,iNumItems)--> not in order?

 
Jump to:  
 
korean_dave
PostPosted: Thu Aug 14, 2008 2:54 pm    Post subject: for y in range (0,iNumItems)--> not in order?
       
for y in range(0,iNumItems):
print(str(y))

How do i make the output go IN ORDER
0
1
2
3
4
5
6
etc.

instead of
0
1
10
11
12
13
14
2
3
4
5
6
7
8
9
 

 
Larry Bates
PostPosted: Thu Aug 14, 2008 2:54 pm    Post subject: Re: for y in range (0,iNumItems)--> not in order?
       
korean_dave wrote:
Quote:
for y in range(0,iNumItems):
print(str(y))

How do i make the output go IN ORDER
0
1
2
3
4
5
6
etc.

instead of
0
1
10
11
12
13
14
2
3
4
5
6
7
8
9

That's not what it does on my system (Python 2.5.2 on Windows). Please post the
code that you are "actually" running.

Quote:
for y in range(15):
.... print str(y)

....
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14

You must have put them in a list and sorted them prior to printing them to get
the output you show. If you sort the list, the output is correct. To make it
different, you will need to pass sort a custom cmp function.

-Larry
 

 
Bruno Desthuilliers
PostPosted: Thu Aug 14, 2008 2:54 pm    Post subject: Re: for y in range (0,iNumItems)--> not in order?
       
korean_dave a écrit :
Quote:
for y in range(0,iNumItems):
print(str(y))

How do i make the output go IN ORDER
0
1
2
3
4
5
6
etc.

instead of
0
1
10
11
12
13
14
2
3
4
5
6
7
8
9

Your code doesn't expose this problem:

bruno@bruno:~$ python
Python 2.5.1 (r251:54863, Mar 7 2008, 03:41:45)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Quote:
for i in range(10):
.... print str(i)

....
0
1
2
3
4
5
6
7
8
9

FWIW, this could be greatly simplified:

nbItems = 10
print "\n".join(map(str, range(nbItems)))

# or if you want something more generic:

print "\n".join("%s" % y for y in range(nbItems))


wrt/ your problem, I suppose you sorted the list *after* having
"converted" ints to strings, since the ordering you have is correct
string ordering.
 

 
Fredrik Lundh
PostPosted: Thu Aug 14, 2008 2:54 pm    Post subject: Re: for y in range (0,iNumItems)--> not in order?
       
korean_dave wrote:

Quote:
Still the same output...

Here's the actual code...

for x in range(0,2):
for y in range(0,27):
for z in range(0,15):
print(str(x) + " " + str(y) + " " + str(z))

umm. that's even less related to your original output than your earlier
code sample.

$ python actual_code.py
0 0 0
0 0 1
0 0 2
0 0 3
0 0 4
....
1 26 10
1 26 11
1 26 12
1 26 13
1 26 14

please do a little more debugging on your own machine before claiming
that fundamental python features that's been around for ages suddenly
won't work.

</F>
 

 
Bruno Desthuilliers
PostPosted: Thu Aug 14, 2008 2:54 pm    Post subject: Re: for y in range (0,iNumItems)--> not in order?
       
korean_dave a écrit :
Quote:
Still the same output...


"same" as what, with what ??? Do yourself (and the world) a favour :
learn to quote the posts you're answering to.

Quote:
Here's the actual code...

for x in range(0,2):
for y in range(0,27):
for z in range(0,15):
print(str(x) + " " + str(y) + " " + str(z))

I fail to see how this code could generate what you described in your
first post. Did you ever bother to *try* this code ? Python ships with
an interactive interpreter , you know, so testing simple code snippets
like the one in your first post or this one above is, well, *very* easy.


Also and FWIW, Python has string formatting and much more to offer.

for x in range(2):
for y in range(27):
for z in range(15):
print "%s %s %s" % (x, y, z)

or

print "\n".join(
"%s %s %s" % (x, y, z)
for x in range(2)
for y in range(27)
for z in range(15)
)
 

 
korean_dave
PostPosted: Thu Aug 14, 2008 3:35 pm    Post subject: Re: for y in range (0,iNumItems)--> not in order?
       
Still the same output...

Here's the actual code...

for x in range(0,2):
for y in range(0,27):
for z in range(0,15):
print(str(x) + " " + str(y) + " " + str(z))
 

 
korean_dave
PostPosted: Thu Aug 14, 2008 3:55 pm    Post subject: Re: for y in range (0,iNumItems)--> not in order?
       
hahah true.

Sorry folks. I am running a third party scripting machine that uses
python. It's the third-party program that is at fault. More detail
involved that you don't need to know, but it's not python's "fault".

Sorry for the inconvenience.

On Aug 14, 11:47 am, Fredrik Lundh <fred...@pythonware.com> wrote:
Quote:
korean_dave wrote:
Still the same output...

Here's the actual code...

    for x in range(0,2):
        for y in range(0,27):
            for z in range(0,15):
                print(str(x) + " " + str(y) + " " + str(z))

umm.  that's even less related to your original output than your earlier
code sample.

$ python actual_code.py
0 0 0
0 0 1
0 0 2
0 0 3
0 0 4
...
1 26 10
1 26 11
1 26 12
1 26 13
1 26 14

please do a little more debugging on your own machine before claiming
that fundamental python features that's been around for ages suddenly
won't work.

/F
 

 
Wojtek Walczak
PostPosted: Thu Aug 14, 2008 3:57 pm    Post subject: Re: for y in range (0,iNumItems)--> not in order?
       
On Thu, 14 Aug 2008 08:35:15 -0700 (PDT), korean_dave wrote:
Quote:
Still the same output...

Here's the actual code...

for x in range(0,2):
for y in range(0,27):
for z in range(0,15):
print(str(x) + " " + str(y) + " " + str(z))

This code won't produce the output you have shown in your
original post.

What's your supposed output for the code above (IOW, what
are you trying to achieve)?

And BTW, which version of python do you use? If it's not
Python 3.0 don't put parentheses around the print instruction,
it's not a function. You could also write:
print "%d %d %d" % (x, y, z)

--
Regards,
Wojtek Walczak,
LINK
 

 
korean_dave
PostPosted: Thu Aug 14, 2008 5:54 pm    Post subject: Re: for y in range (0,iNumItems)--> not in order?
       
Well, here is the whole problem.

It works perfectly. We are using a 3rd party automation program.
Basically instead of print() i used a test.log() command that feeds
into the 3rd party program and gives me output. I can sort output by
date, time, category, etc. As you've seen, the program runs fast. So
we have basically 20 iterations in a second. When i sort by time, it
sorts by string (output) b/c there are 20 outputs in a second.

Thanks all.


On Aug 14, 12:07 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalid> wrote:
Quote:
korean_dave a écrit :

Still the same output...

"same" as what, with what ??? Do yourself (and the world) a favour :
learn to quote the posts you're answering to.

Here's the actual code...

    for x in range(0,2):
        for y in range(0,27):
            for z in range(0,15):
                print(str(x) + " " + str(y) + " " + str(z))

I fail to see how this code could generate what you described in your
first post. Did you ever bother to *try* this code ? Python ships with
an interactive interpreter , you know, so testing simple code snippets
like the one in your first post or this one above is, well, *very* easy.

Also and FWIW, Python has string formatting and much more to offer.

for x in range(2):
     for y in range(27):
         for z in range(15):
             print "%s %s %s" % (x, y, z)

or

print "\n".join(
     "%s %s %s" % (x, y, z)
     for x in range(2)
     for y in range(27)
     for z in range(15)
     )
 

 
Steven D'Aprano
PostPosted: Thu Aug 14, 2008 8:35 pm    Post subject: Re: for y in range (0,iNumItems)--> not in order?
       
On Thu, 14 Aug 2008 10:54:25 -0700, korean_dave wrote:

Quote:
Well, here is the whole problem.

It works perfectly.


I can see why that might be a problem. To solve it, open the source code
in a text editor and make random changes to the code.


--
Steven
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

LechoƄ Jan wiersze nieruchomoƛci lublin O Ɓazickim a Barzym - Kochanowski Jan Podczas ƚwiąt - Lipska Ewa Parkiety