2019-06-01 文件操作练习题

作业:创建文件:data.txt,一共100000行,每行存放一个1-100之间的整数。 (2)找出文件中数字出现最多的10个数字。写入到文件:mostNum.txt中。 collections.Counter

from collections import Counter
count_list = Counter(['B','A','B','A','B','B','A','B','B','C'])
print(count_list)
print(count_list.most_common(2))
count_truple = Counter((1,2,3,1,1,1,2,1,3,3))
print(count_truple)



import random
with open("data.txt",'w+') as f:
    for i in range(100000):
        num = random.randint(1,101);
        f.write(str(num)+ "\n")


with open("data.txt","r+") as f:
    lists = []
    line = f.readline()
    while line:
        lists.append(line)
        line = f.readline()



print(Counter(lists).most_common(10))

你可能感兴趣的:(2019-06-01 文件操作练习题)