Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I get 'dict' has no attribute 'sort' error in my concordance program in Python

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 :[

like image 235
Kim Y Avatar asked Feb 28 '26 13:02

Kim Y


2 Answers

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.

like image 114
0eggxactly Avatar answered Mar 03 '26 03:03

0eggxactly


Do you want myDict to just be a list? If so, declare it as myDict = []. A list has sort and append functions, but a dictionary doesn’t.

like image 24
Squazic Avatar answered Mar 03 '26 02:03

Squazic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!