Frequency: counter.most_common() with operator module error

This is the place for queries that don't fit in any of the other categories.

Frequency: counter.most_common() with operator module error

Postby lovecodecakes » Wed Jun 05, 2013 3:09 pm

I have a text file.

And I print out a list of word frequency as in the code:

Code: Select all
fin = open(r'wordcatch.txt', 'r')

f = (i for i in fin.xreadlines())

word = [w for w in f]

from collections import Counter

counter = collections.Counter(word)

word_freq = tuple(counter.most_common())


=========================================

Error Part:

Code: Select all
import operator

word_freq = tuple((counter.most_common()).sort(key=operator.itemgetter))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-6933670818cc> in <module>()
----> 1 word_freq = tuple((counter.most_common()).sort(key=operator.itemgetter))

TypeError: 'NoneType' object is not iterable


Try printing word_freq before error. And it will be fine.

Q: accordingly, if I want to sort it according to its frequency why is the 'key' part in the sort giving error?
Is there a workaround instead of operator?
Attachments
wordcatch.txt
Simple text file
(7.55 KiB) Downloaded 40 times
lovecodecakes
 
Posts: 56
Joined: Mon Feb 11, 2013 8:19 pm

Re: Frequency: counter.most_common() with operator module er

Postby setrofim » Wed Jun 05, 2013 3:19 pm

You need to call itemgetter with the item you want to get. In your case, that would be the element at index 1, so
Code: Select all
word_freq = tuple((counter.most_common()).sort(key=operator.itemgetter(1)))
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Frequency: counter.most_common() with operator module er

Postby lovecodecakes » Wed Jun 05, 2013 4:54 pm

This is what it gets with itemgetter(1):

Code: Select all
word_freq = tuple((counter.most_common()).sort(key=operator.itemgetter(1)))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-30-4fd9021c8a70> in <module>()
----> 1 word_freq = tuple((counter.most_common()).sort(key=operator.itemgetter(1)))

TypeError: 'NoneType' object is not iterable


Try out the text file attached yourself
lovecodecakes
 
Posts: 56
Joined: Mon Feb 11, 2013 8:19 pm

Re: Frequency: counter.most_common() with operator module er

Postby lovecodecakes » Wed Jun 05, 2013 5:22 pm

Anyway, shame I had to go through this and not a one line solution:

Code: Select all
counter = collections.Counter(word)

word_freq = tuple(counter.most_common())

sorted(word_freq, key = operator.itemgetter(1), reverse = True)


Well this worked only once,

Code: Select all
fin = open(r'C:\Users\Hero\Desktop\my_pys\wordcatch.txt', 'r')
f = (i.split(' ') for i in fin.xreadlines())
word = [i for i in f][0]


Its always confusing how to retrieve a list of words from a string of text. Above is ridiculous! Somebody please enlighten me.
lovecodecakes
 
Posts: 56
Joined: Mon Feb 11, 2013 8:19 pm

Re: Frequency: counter.most_common() with operator module er

Postby micseydel » Wed Jun 05, 2013 8:10 pm

lovecodecakes wrote:
Code: Select all
f = (i.split(' ') for i in fin.xreadlines())
word = [i for i in f][0]

How about
Code: Select all
f.readline().split(' ')
?

I mean, comprehensions are awesome, but they seem kind of silly here.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1256
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Frequency: counter.most_common() with operator module er

Postby lovecodecakes » Wed Jun 05, 2013 8:30 pm

micseydel wrote:How about
Code: Select all
f.readline().split(' ')
?


Awesome readlines and its variants successfully confuse me.
Thanks alot!
lovecodecakes
 
Posts: 56
Joined: Mon Feb 11, 2013 8:19 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 3 guests