数组内容双位数排序:
#coding:utf-8 print u"中文" a = ['15','11','33','52','61','43'] b = 0 c = 0 print a i =0 for j in range (len(a)): for i in range(0,len(a)-1): if a[i] > a[i+1]: b=a[i] a[i]=a[i+1] a[i+1]=b #a.sort() #a.sort(key = len) print a
数组内容不同位数升序: a.sort()方法
#coding:utf-8 print u"中文" a = ['15','1','13','5','61','3'] a.sort() a.sort(key = len) print a
#['1', '3', '5', '13', '15', '61']
sort方法还有两个可选参数:key和reverse
key在使用时必须提供一个排序过程中调用的方法
reverse实现同位数降序排序,需要提供一个布尔值:
y = [3, 2, 8 ,0 , 1] y.sort(reverse = True) print y #[8, 3, 2, 1, 0]