美文网首页
关于sort和Counter方法的应用

关于sort和Counter方法的应用

作者: 不_一 | 来源:发表于2018-03-24 19:43 被阅读0次
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from collections import Counter
import re

with open('a.txt', 'r', encoding='utf-8') as f:
    txt = f.read()
f = lambda x: x.lower()
c = Counter(map(f, re.split('\W+', txt)))  # \W 匹配非字母或数字或下划线
ret = c.most_common(10)
print(ret)

# 按照字符串的多少排序

from collections import Counter, OrderedDict

str1 = 'sageeurjweojgnoer'

s = Counter(str1)
# [('s', 1), ('a', 1), ('g', 2), ('e', 4), ('u', 1), ('r', 2), ('j', 2), ('w', 1), ('o', 2), ('n', 1)]
t = list(s.items())
t.sort(key=lambda x: x[1], reverse=True)
l = []

for k, v in t:
    print(k, v)
    for j in range(v):
        l.append(k)

print(''.join(l))

相关文章

网友评论

      本文标题:关于sort和Counter方法的应用

      本文链接:https://www.haomeiwen.com/subject/zbcccftx.html