欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python开发冒泡排序实方案

程序员文章站 2023-02-27 18:55:03
python开发冒泡排序实方案。 # -*- coding: utf_8 -*- print(__doc__) dic = {} with open('G:/...

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))