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 :[
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.
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.
you can easily sort the order of the dictionary this way:
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()
if not word in myDict:
myDict[word] = []
myDict[word].append(linenum)
print "%-15s %-15s" %("Word", "Line Number")
for key in sorted(myDict):
print '%-15s: %-15d' % (key, myDict(key))
Hope it helps Jordi
dicttype has no sort method. What you can do is call.keys()or.values()(depending on which one you want, naturally) on the dictionary object and sort the result - Joel Cornett 2012-04-05 03:30