-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathword_count.py
More file actions
44 lines (35 loc) · 919 Bytes
/
word_count.py
File metadata and controls
44 lines (35 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
# @Time : 2021/5/14 01:51
# @Author : beyoung
# @Email : linbeyoung@stu.pku.edu.cn
# @File : word_count.py
from collections import Counter
input_path = 'multi_lines.txt'
with open(input_path, 'r') as f:
lines = f.readlines()
new_lines = []
for line in lines:
line = line.replace('/disks/sdd/beyoung/Fakepages/chinese_fonts/', '').replace('\n', '').replace(' ', '')
new_lines.append(line)
dict2 = Counter(new_lines)
sorted_x = sorted(dict2.items(), key=lambda x: x[1], reverse=True)
# print(sorted_x)
for i in sorted_x:
print(i)
# for key, value in dict2.items():
# print('{key}:{value}'.format(key=key, value=value))
'''
b=set(new_lines)
c=[]
for i in b:
c.append([i,new_lines.count(i)])
c.sort(key = lambda x:(x[1], x[0]),reverse = True)
print(c)
'''
'''
# 字典统计
dict = {}
for key in new_lines:
dict[key] = dict.get(key, 0) + 1
print(dict)
'''