开发者

element in dict

开发者 https://www.devze.com 2023-02-07 08:29 出处:网络
I have a dict as below: v0 = {(7, 3): \'7\', (4, 7): \'*\', (1, 3): \'*\', (6, 6): \'*\', (3, 0): \'2\', (2, 8): \'*\', (8, 0): \'3\', (7, 8): \'*\', (5, 4): \'*\', (2, 1):

I have a dict as below:

v0 = {(7, 3): '7', (4, 7): '*', (1, 3): '*', (6, 6): '*', (3, 0):
'2', (2, 8): '*', (8, 0): '3', (7, 8): '*', (5, 4): '*', (2, 1):
'*', (5, 6): '4', (6, 2): '*', (1, 6): '*', (3, 7): '*', (5, 1):
'*', (0, 3): '4', (8, 5): '2', (2, 5): '*', (7, 2): '*', (4, 0):
'*', (1, 2): '2', (3, 8): '6', (6, 7): '*', (3, 3): '1', (0, 6):
'*', (8, 1): '*', (7, 6): '5', (4, 4): '4', (6, 3): '*', (1, 5):
'1', (5, 8): '3', (5, 0): '6', (2, 2): '*', (8, 6): '*', (3, 5):
'*', (4, 1): '*', (1, 1): '*', (6, 4): '*', (3, 2): '7', (0, 0):
'*', (8, 2): '*', (2, 7): '4', (7, 1): '2', (4, 5): '*', (0, 4):
'2', (6, 0): '*', (1, 4): '7', (7, 7): '*', (5, 5): '7', (7, 5):
'*', (2, 3): '*', (0, 7): '*', (8, 7): '7', (6, 8): '*', (4, 2):
'*', (1, 0): '*', (0, 8): '5', (5, 7): '*', (6, 5): '*', (5, 3):
'*', (0, 1): '1', (8, 3): '*', (7, 0): '1', (4, 6): '*', (3, 6):
'*', (3, 4): '*', (6, 1): '7', (3, 1): '*', (2, 6): '*', (2, 4):
'*', (7, 4): '3', (2, 0): '*', (1, 8): '9', (8, 8): '*', (4, 3):
'*', (1, 7): '3', (0, 5): '*', (4, 8): '*', (5, 2): '*', (0, 2):
'*', (8, 4): '8'}

Is there a 开发者_Go百科way I can retrieve any particular key of my interest?

Say for example, I will give an input 7 & 0 to my function and it should get me the result:

1.

Or if I give 8 & 4 it should get me 8.

I'm trying to learn Python. Anything to help me solve my question be greatly appreciated.

Thanks.


Have you tried this?

>>> v0[(7,3)]
'7'
>>> v0[(7,0)]
'1'

Or in a function:

>>> def retr(x, y):
...     return v0[(x, y)]
... 
>>> retr(8,4)
'8'

EDIT:

Technically speaking, you do not have to use the parentheses within the braces, but it does make it clearer that the dictionary key is in fact a single tuple.

>>> v0[7,0]
'1'


Yes of course, that's what a dictionary is! if your dictionary is called v0 then you just use v0[(7,0)] or v0[(8,4)]

In this case, (7,0) and (8,4) are the "keys" and what they map to, e.g. 1 and 8, respectively, are the "values" of a dictionary. A dictionary is useful for storing values when you want to retrieve them by key, just like you're doing now.


the great thing in dictionaries is that you always work with key value pairs, i.e. (7, 0) is your key and 1 is the value corresponding to it. So you can access the value by calling

value = v0[key]

which is, for your example

value = v0[(7, 0)]

and value will have the value 1 after that operation.

See here for more information about dictionaries.


And they looked like coordinates to me. If you are trying to represent a matrix, use a multi-dimensional array instead of a dictionary. It's easier to use, and also a faster solution.

For multi-dimensional arrays, follow that link: http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch20s05.html

0

精彩评论

暂无评论...
验证码 换一张
取 消