Goose
Goose

Reputation: 2250

How to print elements in a list in new lines?

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.

Upvotes: 4

Views: 16266

Answers (4)

Juan Cortés
Juan Cortés

Reputation: 21132

I'm surprised that nobody suggested using the unpacking operator *, since you say python3 so why not do the following, you can test it here too.

print(*[x[0]for x in L.most_common(10)], sep="\n")

Related questions

Upvotes: 1

abarnert
abarnert

Reputation: 366133

If you just want the strings:

print("\n".join(element for element, count in c.most_common(10)))

If you want the strings and the counts printed in the form ('foo', 11):

print ("\n".join(str(element_and_count) 
       for element_and_count in c.most_common(10)))

If you want the strings and counts in some other format of your choice:

print ("\n".join("{}: {}".format(element, count) 
       for element, count in c.most_common(10)))

Why? The most_common function returns (element, count) pairs. Those things are tuples, not strings. You can't just join tuples together. You can, of course, convert it to a string (option #2 above), but that only works if you actually want the format ('foo', 11) for each line. To get the other two options, you want to ignore half the tuple and use the other, or write your own format expression.

In any case, you want to do something to each member of the sequence returned by most_common. The Pythonic way to do that is with a list comprehension or generator expression.

Meanwhile, you should learn how to debug these kinds of cases. When join gives you a TypeError, break it up into pieces until you find the one that stores working (and try it with 2 instead of 10, just so there's less to read):

>>> print("\n".join(c.most_common(2)))
TypeError: sequence item 0: expected str instance, tuple found
>>> c.most_common(2)
[('I', 4), ('man', 1)]

Aha! Each thing in the list is a tuple of two things, not just a string. Why?

>>> help(c.most_common)
most_common(self, n=None) method of collections.Counter instance
    List the n most common elements and their counts from the most
    common to the least.  If n is None, then list all element counts.

    >>> Counter('abcdeabcdabcaba').most_common(3)
    [('a', 5), ('b', 4), ('c', 3)]

OK, so it returns the most common elements and their counts. I just want the elements. So:

>>> [element for element, count in c.most_common(2)]
['I', 'man']

Now that's something I can join:

>>> '\n'.join([element for element, count in c.most_common(2)])
'I\nman'

And I don't need both brackets and parents (I can just use an expression instead of a list comprehension):

>>> '\n'.join(element for element, count in c.most_common(2))
'I\nman'

And now, I can print it:

>>> print('\n'.join(element for element, count in c.most_common(2)))
I
man

And now that it's working, print all 10:

>>> print('\n'.join(element for element, count in c.most_common(10)))

Upvotes: 1

Jon Clements
Jon Clements

Reputation: 142256

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))

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304473

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)))

Upvotes: 5

Related Questions