首页 > 程序开发 > Web开发 > Python >

python开发冒泡排序实方案

2017-11-01

python开发冒泡排序实方案。

python开发冒泡排序实方案。

# -*- coding: utf_8  -*-
print(__doc__)
dic = {}
with open('G:/py_proj/test/flask.txt') as f:
     for char in   f.read().replace(' ',''):
        if char in dic:
            dic[char] += 1
        else:
            dic[char] = 1
# print(dic)
rev_list = list(dic.items())
length = len(rev_list)
# 冒泡排序
for i in range(length-1):
    for j in range(length-i-1):  #j为下标识
        if rev_list[j][1] > rev_list[j+1][1]:
            rev_list[j],rev_list[j+1] = rev_list[j+1],rev_list[j]
print(rev_list)
for char ,c  in sorted(rev_list,key= lambda x:x[1],reverse= True):
    print('%s count is %d' %(char,c))
相关文章
最新文章
热点推荐