python 对字典进行排序
#!/usr/bin/python#coding:utf-8import stringa = dict()m = 0for i in string.ascii_lowercase:a[i] = mm +=1a.iteritems()print a.items()print len(a.items())tt = a.items()#字典排序算法一#
·
#!/usr/bin/python
#coding:utf-8
import string
a = dict()
m = 0
for i in string.ascii_lowercase:
a[i] = m
m +=1
a.iteritems()
print a.items()
print len(a.items())
tt = a.items()
#字典排序算法一
#######################
#下面进行冒泡排序
for i in range(len(tt)):
for m in range(len(tt) -1):
if tt[m][1] < tt[m+1][1]:
tt[m],tt[m+1] = tt[m+1],tt[m]
print tt
#######################
#方法2(排序法)
from operator import itemgetter
dict_t = a.items()
gg = sorted(dict_t,key=itemgetter(1),reverse = True)
print gg
#方法三(排序法)
dict_r = a.items()
ll = sorted(dict_r,key=lambda t:t[1] ,reverse = True)
print ll
更多推荐
已为社区贡献15条内容
所有评论(0)