With this code i print all the elements sorted with the most common word used in the textfile first. But how do i print the first ten elements?
with open("something.txt") as f:
words = Counter(f.read().split())
print(words)
From the docs:
most_common([n])
Return a list of the n most common elements and their counts from the most common to the least. If n is omitted or None, most_common() returns all elements in the counter. Elements with equal counts are ordered arbitrarily:
I would try:
words = Counter(f.read().split()).most_common(10)
Source: here
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