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

  • SEARCH
  • Home
  • 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 7744473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:46:35+00:00 2026-06-01T09:46:35+00:00

The task is to write a program which prompts for a filename and then

  • 0

The task is to write a program which prompts for a filename and then produces a concordance of that file.
Ex. A concordance is an alphabetical index that shows the lines in a document where each word occurs. For example, a concordance for this paragraph might appear as:

Word          Line Number
a             1 1 2
alphabetical  1
an            1
appear        2

Here I make a list so that I can sort the words.

I have this code:

f = open(raw_input("Enter a filename: "), "r")
myDict = {}
linenum = 0

for line in f:
line = line.strip()
line = line.lower()
line = line.split()
linenum += 1

for word in line:
    word = word.strip()
    word = word.lower()
    myDict[word] = linenum

    if word in myDict:
        myDict.sort()
    else:
        myDict.append(word)

print "%-15s %-15s" %("Word", "Line Number")
print "%-15s %-15d" %(myDict.keys(), myDict.values())

When I run the program now it says ‘dict’ has no attribute ‘sort’. Can you explain this please?

The file is the same as the example and the output should also be the example from above. I’m very new at python please help :[

  • 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-06-01T09:46:36+00:00Added an answer on June 1, 2026 at 9:46 am

    I think it makes sense to use a dict, but you’ll have to add a key along with each value you add to the dict. For example:

    >>> dict = {}
    >>> dict["apple"] = "red"
    >>> dict["banana"] = "yellow"
    >>> dict
    {'apple': 'red', 'banana': 'yellow'}
    

    In this example, the keys are “apple” and “banana”, and the values are “red” and “yellow”. Since this is homework, I’ll leave it up to you to determine appropriate keys and values for your assignment.

    Also, this line is problematic:

    for word in line:
    

    line is a string, so you’re actually looking at each character in line, rather than each word. You’ll have to find some way to transform line into a list of words…

    Lastly, your final statement will only print the last word read. You’re building a dict, but you’re not printing the dict, you’re printing a single value. Once you build the dict, you should print the dict itself.


    myDict[word] = linenum
    
    if word in myDict:
        myDict.sort()
    else:
        myDict.append(word)
    

    You’re on the right path, but sorting the dictionary isn’t the right way to handle words that appear more than once (furthermore, dict doesn’t have a sort method, which is why you’re getting an error, but even if it did, you wouldn’t need it here). Also, once you assign a value to a key, it’s added to the dictionary, so it’s already been “appended”.

    In your example, the word a appears 3 times, and the output lists each line it appears in, so you’ll need a way to store a list of lines for each word.

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

Sidebar

Related Questions

I have been assigned wit the task to write a program that takes a
I have task to write program allowing users to draw stars, which can differ
I need to write a program that displays a PDF which a third-party supplies.
If we need to write a program that works periodically, which way do we
I have an interesting task: to write a program which captures input from the
I have a task to write a program which handles the division by zero
My task is to write an app(unfortunatly on C) which reads expression in infix
My task is to write a class RFind which recursively searches the current directory
How to write an ant task that removes files from a previously compiled JAR?
I have a program which performs a useful task. Now I want to produce

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.