Small problem with print and comma

faulkner faulkner612 at comcast.net
Sun Jul 30 18:52:19 EDT 2006


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]

benjamin.cordes at blawrc.de wrote:
> 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[i],
>    #works
>    #for i in range(0,len(param)-1):
>    #	print a[i],
>    #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[i]>key:
> 			a[i+1] = a[i]
> 			i = i-1
> 		a[i+1] = key
> 		
> 	print "\n sorted list"
> 	printList(a)




More information about the Python-list mailing list