[Tutor] Help with loops
Chelsea G
cegarcia0323 at gmail.com
Tue Jan 5 12:27:20 EST 2016
I need to create a loop to print out the most common phrases with the
counts 5 or greater and the rest to be bucketed into other category. How do
I write the loop to print out the common phrases that have counts of 5 or
more? This is my code so far:
import csv#from sys import argvfrom collections import defaultdictfrom
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()
More information about the Tutor
mailing list