简体   繁体   English

访问 python 中的字典值背后的逻辑

[英]Logic behind accessing dictionary values in python

Rookie here and I couldn't find a proper explanation for this.新手在这里,我找不到合适的解释。

We have a simple dict:我们有一个简单的字典:

a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}

to loop through and access the values of this dict I have to call循环并访问我必须调用的这个字典的值

for key in a_dict:
    print(key, '->', a_dict[key])

I am saying about我说的是

a_dict[key]

specifically.具体来说。 Why python use this convention?为什么 python 使用这个约定? Where is a logic behind this?这背后的逻辑在哪里? When I want to get values of a dictionary I should call it something like当我想获取字典的值时,我应该将其称为

a_dict[value] or a_dict[values] etc

instead (thinking logically).相反(逻辑思考)。

Could anyone explain it to make more sense please?有人可以解释一下吗?

edit:编辑:

to be clear: why python use a_dict[key] to access dict VALUE instead of a_dict[value].要明确:为什么 python 使用 a_dict[key] 来访问 dict VALUE 而不是 a_dict[value]。 LOGICALLY.逻辑上。

I think you are misunderstanding some terminology around dictionaries:我认为您误解了有关字典的一些术语:

In your example, your keys are color , fruit , and pet .在您的示例中,您的键是colorfruitpet Your values are blue , apple , and dog .你的价值观是blueappledog

In python, you access your values by calling a_dict[key] , for example a_dict["color"] will return "blue" .在 python 中,您可以通过调用a_dict[key]来访问您的值,例如a_dict["color"]将返回"blue"

If python instead used your suggested method of a_dict[value] , you would have to know what your value was before trying to access it, eg a_dict["blue"] would be needed to get "blue" , which makes very little sense.如果 python 改为使用您建议的a_dict[value]方法,则在尝试访问它之前您必须知道您的值是什么,例如a_dict["blue"]才能获得"blue" ,这没有什么意义。

As in Feras's answer, try reading up more on how dictionaries work here正如在 Feras 的回答中,尝试阅读更多关于字典如何在这里工作的信息

according to your question, I think you meant why python does not use index instead of key to reach values in the dict.根据您的问题,我认为您的意思是为什么 python 不使用索引而不是键来达到字典中的值。

Please take note that there are 4 main data container in python, and each for its usage.请注意 python 中有 4 个主要数据容器,每个容器都有其用途。 (there are also other containers like counter and...) (还有其他容器,如柜台和......)

for example elements of list and tuple is reachable by their indices.例如 list 和 tuple 的元素可以通过它们的索引来访问。

a = [1,2,3,4,5]
print(a[0]) would print 1

but dictionary as its name shows, maps from some objects (keys in python terminology) to some other objects(values in python terminology).但是字典正如其名称所示,从一些对象(python 术语中的键)映射到其他一些对象(python 术语中的值)。 so we would call the key instead of index and the output would be the value.所以我们会调用键而不是索引,而 output 将是值。

a = { 'a':1 , 'b':2 }
print(a['a']) would print 1

I hope it makes it a bit more clear for you.我希望它能让你更清楚一点。

Its because, a dictionary in python, maps the keys and values with a hash function internally in the memory.这是因为,python 中的字典在 ZCD69B4957F06CD8218D 内部使用 hash function映射键和值

Thus, to get the value, you've to pass in the key .因此,要获取值,您必须传入key

You can sort of think it like indices of the list vs the elements of the list, now to extract a particular element, you would use lst[index] ;您可以将其视为列表的索引与列表的元素,现在要提取特定元素,您将使用lst[index] this is the same way dictionaries work;这与字典的工作方式相同; instead of passing in index you would've to pass in the key you used in the dictionary, like dict[key] .而不是传入索引,你必须传入你在字典中使用的key ,比如dict[key]

One more comparison is the dictionary (the one with words and meanings), in that the meanings are mapped to the words, now you would of course search for the word and not the meaning given to the word, directly.另一个比较是字典(具有单词和含义的字典),因为含义映射到单词,现在您当然会直接搜索word而不是单词的meaning

You are searching for a value wich you don't know if it exists or not in the dict, so the a_dict[key] is logic and correct您正在搜索一个您不知道它是否存在于字典中的值,因此 a_dict[key] 是逻辑且正确的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM