#!/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

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐