with open('earpa001.txt',mode='r',encoding='utf-8') as f,\ open('earpa001_count.txt',mode='w',encoding='utf-8') as f1: d = {} content = f.readlines() for lines in content: line = lines.strip('\n').split(',') for n in line[3]: d[n] = d.get(n,0)+1 ls = list(d.items()) ls.sort(key=lambda x:x[1],reverse=True) for i in range(len(ls)): f1.write('1-{},{}'.format(ls[i][0],ls[i][1])+'\n')
a = [1,2,3,4,5,6,7,8,6,5,4,3,2] d= {} for n in a: d[n] = d.get(n,0)+1 ls = list(d.items()) print(d) #{1: 1, 2: 2, 3: 2, 4: 2, 5: 2, 6: 2, 7: 1, 8: 1} print(ls) #[(1, 1), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 1), (8, 1)] ls.sort(key=lambda x:x[1],reverse=True) a,b = ls[0] print('{}:{}'.format(a,b)) #