Python reconciliation file

The challenge is to write a program that asks for a filename and then builds consistency on that file. Ex. Concordance is an alphabetical index that shows the lines in a document where each word occurs. For example, the alignment for this paragraph might look like:

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

      

Here I am creating a list to 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, it now says that the "dict" has no "sort" attribute. Can you explain this?

The file is the same as in the example and the output should also be the example above. I am very new to python, please help: [

+3


source to share


3 answers


I think it makes sense to use a dict, but you will need to add the key along with every value you add to the dict. For example:

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

      

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

Also, this line is problematic:

for word in line:

      

line

is a string, so you are actually looking at every character in line

, not every word. You will need to find a way to convert line

to a list of words ...



Finally, your final statement will only print the last word read. You are building a dict, but you are not printing a dict, you are printing a single value. When 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 track, but sorting a dictionary is the wrong way to handle words that appear more than once (besides, dict doesn't have a sort method, so you get an error, but even if it were, you wouldn't need it here). Also, as soon as you assign a value to a key, it is added to the dictionary, so it is already "added".

In your example, the word a appears 3 times, and the output shows every line it appears on, so you need to keep a list of lines for each word.

+2


source


Do you want myDict to be just a list? If so, declare it as myDict = []. The list has sort and add functionality, but the dictionary does not.



+1


source


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 this helps Jordi

0


source







All Articles