Monday, 14 January 2008

sort within Python

1.
a=[15,13,17,11]
b=list(a)
b.sort()
c=list(b)
for i in range(len(b)):
c[i]=a.index(b[i])

print c

-> [3, 1, 0, 2]

2.
a=[15,13,17,11]
c=list( [ a[i],i ] for i in range (len(a)) )
c.sort()
c
-> [[11, 3], [13, 1], [15, 0], [17, 2]]

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter