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

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Thu Aug 14 11:09:13 EDT 2008


korean_dave a écrit :
> 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 at 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.
 >>> 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.





More information about the Python-list mailing list