I have a list
L = Counter(mywords)
Where
mywords = ['Well', 'Jim', 'opportunity', 'I', 'Governor', 'University', 'Denver', 'hospitality', 'There', 'lot', 'points', 'I', 'make', 'tonight', 'important', '20', 'years', 'ago', 'I', 'luckiest', 'man', 'earth', 'Michelle', 'agreed', 'marry', '(Laughter)', 'And', 'I', 'Sweetie', 'happy']
It's much longer than that but that's a snippet.
Now what I do next is:
print ("\n".join(c.most_common(10)))
Because I want it to show the 10 most commonly used words in that list AND their counts, but I want it to print out into new lines for each item in the list, instead I get this error:
TypeError: sequence item 0: expected str instance, tuple found
Any help would be appreciated, using Python 3.
print ("\n".join(map(str, c.most_common(10))))
If you want more control over the format, you can use a format string like this
print ("\n".join("{}: {}".format(k,v) for k,v in c.most_common(10)))
The simplest is:
for item, freq in L.most_common(10):
print(item, 'has a count of', freq) # or
print('there are {} occurrences of "{}"'.format(freq, item))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With