Jump to content

Python 3 fails and I can't see why.

AC-3
Go to solution Solved by vlads_,
dic.get([words[1], 0])

on your 5th line should be

dic.get(words[1], 0)

Instead of calling get with 2 arguments: the key to use (words[1]) and the default value if the key is not found (0), you are calling get with one argument: a new list with these 2 elements. Python tries to use it as a key, but it fails, which is what it's complaining about.

I'm doing a course right now to learn Python, and I can't get my head around why my first try fails, and why my solution doesn't. I have already completed the assignment, so no one is doing my homework.

 

dic = dict()
for line in handle:
    if not line.startswith("From: ") : continue
    words = line.split()
    dic[words[1]] = dic.get([words[1], 0]) + 1

This fails with the following traceback:

Traceback (most recent call last):
  File "9.4 Autograder.py", line 9, in <module>
    dic[words[1]] = dic.get([words[1], 0]) + 1
TypeError: unhashable type: 'list'

But,

dic = dict()
for line in handle:
    if not line.startswith("From: ") : continue
    dic[line.split()[1]] = dic.get(line.split()[1], 0) + 1

This one is successful.

 

Any help to make me understand my problem is greatly appreciated!

Link to comment
Share on other sites

Link to post
Share on other sites

dic.get([words[1], 0])

on your 5th line should be

dic.get(words[1], 0)

Instead of calling get with 2 arguments: the key to use (words[1]) and the default value if the key is not found (0), you are calling get with one argument: a new list with these 2 elements. Python tries to use it as a key, but it fails, which is what it's complaining about.

The Eight Fallacies of Distributed Computing

Essentially everyone, when they first build a distributed application, makes the following eight assumptions. All prove to be false in the long run and all cause big trouble and painful learning experiences.

  1. The network is reliable
  2. Latency is zero
  3. Bandwidth is infinite
  4. The network is secure
  5. Topology doesn’t change
  6. There is one administrator
  7. Transport cost is zero
  8. The network is homogeneous

        — Peter Deutsch

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, vlads_ said:

dic.get([words[1], 0])

on your 5th line should be


dic.get(words[1], 0)

Instead of calling get with 2 arguments: the key to use (words[1]) and the default value if the key is not found (0), you are calling get with one argument: a new list with these 2 elements. Python tries to use it as a key, but it fails, which is what it's complaining about.

Thank you very much for the help!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×