#!/usr/bin/python import sys list = [] current_word = None count_pool = [] sum = 0 file1 = 'd:/share/1business.seg.txt' file = open(file1, 'r', encoding='UTF-8') for line in file: ss = line.strip().split(' ') for s in ss: if s.strip() != "": #print "%s\t%s" % (s, 1) y = [s, 1] list.append(y) out_data = sorted(list, key=lambda x: x[0]) for line in out_data: word = line[0] val = line[1] if current_word == None: current_word = word if current_word != word: for count in count_pool: sum += count print("%s\t%s" % (current_word, sum)) current_word = word count_pool = [] sum = 0 count_pool.append(int(val)) for count in count_pool: sum += count print("%s\t%s" % (current_word, str(sum)))
Countword
最新推荐文章于 2023-01-18 23:03:22 发布