views:

62

answers:

2

I made a dictionary, then split up the values and keys into lists and now its looks like this:

keys = [(4,5),(5,6),(4,8)......so on].
values = [('west',1),('south',1).......]

Then I made a new dictionary like in this way,

final = dict((k,v[0]) for k,v in zip(keys, values))

When I execute -print final - output is in this form... {(4,5):west,(5,6):south,......so on}

Now i need to have the value of key (4,5)...it can be any key..

q:2

win = gap.pop() - here gap is a stack
         print win      - the output is (1,1)
         return final.get(win) -

but when I do this return, it gives me an error and final is the directory that I have made with lists of keys and values

The error is: 'W'

+2  A: 

Works for me:

>>> keys=[(4,5),(5,6)]
>>> values = ["west","south"]
>>> f=dict(zip(keys,values))
>>> f
{(4, 5): 'west', (5, 6): 'south'}
>>> f[(4,5)]
'west'
adamk
KeyError: (5, 3)This is wat i am getting
Shilpa
@Shilpa: Well, then the key `(5,3)` is obviously not in your dictionary. From the code you posted so far it seems you have `(a,b)`, where `a < b`, but `5` is larger than `3`. If you print `final`, do you see a key `(5,3)`? I bet not ;)
Felix Kling
yes,it has....but now i solved the problem..thanks
Shilpa
+2  A: 

Works for me:

>>> final = {(4,5):"West", (5,6): "East"}
>>> print final
{(4, 5): 'West', (5, 6): 'East'}
>>> final[(4,5)]
'West'

You might want to try final.get((4,5)).

Or post more code, maybe you do something fancy with final. If you don't get a value back, you should at least get a KeyError:

>>> final[(7,8)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: (7, 8)

In this case you either have to handle the exception:

try:
    final[(7,8)]
except KeyError:
    print "Key not in dict."

or use final.get((7,8), <default value>) which will return <default value> if the key is not found (or None if you don't specify a default value).


Read about dictionaries in the Python documentation.

Felix Kling
`print final[(4,5)]` gives me `west` as well. There must be something missing from the OP's question.
Johnsyweb
Awesome....final.get((4,5)) works for me...thanks But i have 1 more question related to this thing...I am editing the question...plz see it and tell me the right way of doing it...once again..thanks
Shilpa
@Shilpa: You're welcome, but it seems you have not stated your problem correctly. First you said, `final[(4,5)]` does not give you `west` but we could show you that it does. Later in a comment you say that you get a key error for `(5,3)`, but you never mentioned this in your question. Please be *specific* about your problem, we don't want to *guess*.
Felix Kling
Actually the problem is that I am not getting the result for any number. Only the final.get gives me the result. I dont see, weher I used (5,3)..bt leave it now, its not an issue....I need to find the soln of my next error.....why is that so ?/
Shilpa
@Shilpa: You should create a new question and post more complete code. Your code pieces are to small and we have to guess a lot.
Felix Kling