读取文件,将每一行作为列表的一个值,求每个字符串出现的次数,以及有哪些字符串。
写入文件,按次数大小排序,次数后面对应着字符串
# -*- coding:utf-8 -*-
input1 = open('jpc.txt', 'r')
output1 = open('out.txt', 'w')
list_of_all_the_lines = input1.read()
a = list_of_all_the_lines.splitlines(False)
myset = set(a)
mylist_cnt = []
mylist_name = []
for item in myset:
mylist_cnt.append(a.count(item))
mylist_name.append(item)
for i in range(len(mylist_cnt)-1, 1, -1):
for j in range(0, i):
if mylist_cnt[j] < mylist_cnt[j+1]:
mylist_cnt[j], mylist_cnt[j+1] = mylist_cnt[j+1], mylist_cnt[j]
mylist_name[j], mylist_name[j+1] = mylist_name[j+1], mylist_name[j]
for res in range(len(mylist_cnt)):
# print("%s\t\t%s" % (mylist_cnt[res], mylist_name[res]))
s = str(mylist_cnt[res])
s += "\t\t"
s += str(mylist_name[res])
s += "\r\n"
output1.write(s)
input1.close()
output1.close()