Small problem with print and comma

B

benjamin.cordes

Hi,

I have a small problem with my function: printList. I use print with a
',' . Somehow the last digit of the last number isn't printed. I wonder
why.

import random

def createRandomList(param):
length = param

a = []
"""" creating random list"""
for i in range(0,length):
a.append(random.randrange(100))
return a

def printList(param):
#doesn't work
#2 sample outputs
# 30 70 68 6 48 60 29 48 30 38
#sorted list
#6 29 30 30 38 48 48 60 68 7 <--- last character missing

#93 8 10 28 94 4 26 41 72 6
#sorted list
#4 6 8 10 26 28 41 72 93 9 <-- dito


for i in range(0,len(param)):
print a,
#works
#for i in range(0,len(param)-1):
# print a,
#print a[len(param)-1]


if __name__ == "__main__":
length = 10
a = createRandomList(length)
printList(a)

for j in range(1,len(a)):
key = a[j]
i = j-1
while i > -1 and a>key:
a[i+1] = a
i = i-1
a[i+1] = key

print "\n sorted list"
printList(a)
 
F

faulkner

why don't you iterate over the list instead of indices?
for elem in L: print elem,

you don't need the 0 when you call range: range(0, n) == range(n)
the last element of a range is n-1: range(n)[-1] == n-1
you don't need while to iterate backwards. the third argument to range
is step.
range(n-1, -1, -1) == [n-1, n-2, n-3, ..., 1, 0]
 
T

Tim Chase

I have a small problem with my function: printList. I use print with a
',' . Somehow the last digit of the last number isn't printed. I wonder
why.

Posting actual code might help...the code you sent has a horrible
mix of tabs and spaces. You've also got some craziness in your
"creating random list" string. First off, it looks like you're
using a docstring, but they only go immediately after the def
line. I'd recommend putting it where it belongs, or changing the
line to a comment.

There are some unpythonic bits in here:

printList() would usually just idiomatically be written as

print ' '.join(a)

although there are some int-to-string problems with that, so it
would be written as something like

print ' '.join([str(x) for x in listOfNumbers])

which is efficient, and avoids the possibility of off-by-one
errors when range(0,length).

Another idiom would be the list-building of createRandomList:

return [random.randrange(100) for x in xrange(0,length)]

Additionally, this can be reduced as range/xrange assume 0 as the
default starting point

return [random.randrange(100) for x in xrange(length)]

(using xrange also avoids building an unneeded list, just to
throw it away)

Additionally, rather than rolling your own bubble-sort, you can
just make use of a list's sort() method:

a.sort()

Other items include sensibly naming your parameters rather than
generically calling them "param", just to reassign them to
another name inside.

Taking my suggestions into consideration, your original program
condenses to

########################################

import random

def createRandomList(length):
return [random.randrange(100) for x in xrange(length)]

def printList(listOfNumbers):
print ' '.join([str(x) for x in listOfNumbers])

if __name__ == "__main__":
length = 10
a = createRandomList(length)
printList(a)
a.sort()
print "sorted list"
printList(a)

########################################

one might even change createRandomList to allow a little more
flexibility:

def createRandomList(length, maximum=100):
return [random.randrange(maximum) for x in xrange(length)]


So it can be called as you already do, or you can specify the
maximum as well with

createRandomList(10, 42)

for future use when 100 doesn't cut it for you in all cases.

Just a few thoughts.

-tkc
 
D

Dustan

Dennis said:
for i in range(0,len(param)):
print a,


for it in param:
print it,


That's one way. However, if you need the position (this is for future
reference; you don't need the position number here):

for i in range(len(param)+1):
print a,

The last position was excluded because you forgot the '+1' part,
creating an off-by-one bug.
 
D

Dennis Lee Bieber

for i in range(len(param)+1):
print a,

The last position was excluded because you forgot the '+1' part,
creating an off-by-one bug.


No... adding 1 WILL create an off-by-one (in the other direction)
error.
lst = [1, 3, 5, 3.1415926, "a string"]
for i in range(len(lst)):
.... print lst,
....
1 3 5 3.1415926 a string.... print lst,
....
1 3 5 3.1415926 a stringTraceback (most recent call last):
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Diez B. Roggisch

Dustan said:
Dennis said:
for i in range(0,len(param)):
print a,


for it in param:
print it,


That's one way. However, if you need the position (this is for future
reference; you don't need the position number here):

for i in range(len(param)+1):
print a,

The last position was excluded because you forgot the '+1' part,
creating an off-by-one bug.


No, your code creates that bug.

However, the above is not very pythonic - if param is a iterator and not a
sequence-protocol-adherent object, it fails. The usual way to do it is


for i, a in enumerate(param):
print a,


Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top