required help in manual sort

Manjunath N manj.ttd at gmail.com
Mon Mar 7 03:48:46 EST 2011


Hello users,
     I'm quite new to python programming.  I need help in manually sorting a
list which is shuffled.  The problem i'm facing is with respect to last
element in the list when checking the condition using if statement. Below I
have pasted my code. The code is below is not yet done, at first I am trying
to separate the number as lower and higher.  I just need help here.

import random, pdb

pdb.set_trace()

unordered=range(10)
random.shuffle(unordered)
ordered=[]

print "The unordered selection of list is\n",unordered,"\n"

unordered_len=len(unordered)
higher=[]
lowest=[]
a=1

for i in unordered:
    if i<unordered[a]:
        lowest.append(i)
        a+=1
    elif i > unordered[a]:
        higher.append(i)
        a+=1

print "\n\n\nLowest",lowest
print "\n\nHighest",higher

 When I run the program, I get the error message as,
line 16, in <module>
    if i<unordered[a]:
IndexError: list index out of range

I know that the error is happening because when variable 'i' comes to the
last element in the unordered list, it doesnt have anyother variable for
checking the condition if i<unordered[a]: .  But i'm not able to handle this
condition in the code.

So someone please help in this regards.

Thanks,
Manjunath
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110307/542e09c9/attachment.html>


More information about the Python-list mailing list