Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6738769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:24:28+00:00 2026-05-26T11:24:28+00:00

Working on a word occurrence count application in a Python 3.2 / Windows environment.

  • 0

Working on a word occurrence count application in a Python 3.2 / Windows environment.

Can anyone please help to tell me why the following isn’t working?

from string import punctuation
from operator import itemgetter

N = 100
words = {}

words_gen = (word.strip(punctuation).lower() for line in open("poi_run.txt")
                                         for word in line.split())

for word in words_gen:
    words[word] = words.get(word, 0) + 1

top_words = (words.iteritems(), key=itemgetter(1), reverse=True)[:N]

for word, frequency in top_words:
    print ("%s %d") % (word, frequency)

The trace back error is:

Message File Name   Line    Position    
Traceback               
    <module>    C:\Users\will\Desktop\word_count.py 13      
AttributeError: 'dict' object has no attribute 'iteritems'              

Thanks

n.b.

Fully working code:

from string import punctuation
from operator import itemgetter

N = 100
words = {}

words_gen = (word.strip(punctuation).lower() for line in open("poi_run.txt")
                                         for word in line.split())

for word in words_gen:
    words[word] = words.get(word, 0) + 1

top_words = sorted(words.items(), key=itemgetter(1), reverse=True)[:N]

for word, frequency in top_words:
    print ("%s %d" % (word, frequency))

Thanks again guys

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:24:28+00:00Added an answer on May 26, 2026 at 11:24 am

    In Python 3, use just items where you’d previously use iteritems.

    The new items() returns a dictionary view object that supports iteration as well as len and in.

    And of course, in top_words = (words.iteritems(), ... you forgot to call the sorted function.


    Edit: Please see my other answer for a better solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with Python, and I'm trying to find out if you can tell
we can assume the user is working with Ms Word files. After the user
I'm working on a project where I can generate Word documents, one of the
The following is working as expected. I want to replace the word me with
Is there something I can do or consider when working with Word files in
I am working on a query so that I can compare a word read
I have the following working on Word 2007 on PC: =SUM(ABOVE) <- this cell
:) I tried using w = Word(printables), but it isn't working. How should I
I am working on MS Word application capturing tool in which we capture selected
I working with legacy (ancient is more appropriate word) application which use temp tables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.