<lo: ="" j="lo" <a[j]):="" a[j],a[j+1]="a[j+1],a[j]"
-
-
def mySort(a):
-
lo=len(a)-1
-
i=0
-
while i<lo+1:
-
j=lo
-
while j>=i:
-
if(a[j-1]>a[j]):
-
a[j],a[j-1]=a[j-1],a[j]
-
j=j-1
-
i=i+1
-
if __name__ == '__main__':
-
l1=[3,2]
-
mySort(l1)
-
print l1
字典排序:
d1={'y': 2, 'x': 1, 'z': 3,'a':99,'c':23,'f':15}
字典转换序列:
c1=d1.items()
print c1
[('a', 99), ('c', 23), ('f', 15), ('x', 1), ('y', 2), ('z', 3)]
key,values位置互换:
c2=[]
for i in c1:
c2.append((i[1],i[0]))
In [15]: print c2
[(99, 'a'), (23, 'c'), (15, 'f'), (2, 'y'), (1, 'x'), (3, 'z')]
重新生成字典:
d2={}
for i in c2:
d2[i[0]]=i[1]
In [16]: print d2
{1: 'x', 2: 'y', 99: 'a', 15: 'f', 3: 'z', 23: 'c'}
进行排序:
for i in sorted(d2.keys()):
print d2[i],':',i
x : 1
y : 2
z : 3
f : 15
c : 23
a : 99
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/20747382/viewspace-2135524/
,如需转载,请注明出处,否则将追究法律责任。