我需要创建一个循环来打印最常见的短语,其中计数为5或更大,其余的则被分成其他类别。如何编写循环以打印出计数为5或更多的常用短语?到目前为止,这是我的代码:
import csv
#from sys import argv
from collections import defaultdict
from collections import Counter
#script, filename = argv
data = defaultdict(list)
class dictionary:
with open ('practice.csv', 'rb') as f:
reader = csv.reader(f)
#text_file = open("output.txt", "w")
next(reader, None)
for row in reader:
data[row[2]].append(row[3])
#text_file.write("%r" % data)
#text_file.close()
#print(data)
text_file = open("count.txt", "w")
data_count = Counter()
for d in data.values():
data_count += Counter(d)
print data_count.most_common(5)
text_file.write("%r" % data_count.most_common(5))
text_file.close()