[英]Python - how does a variable from this function progress through a dictionary?
I am having difficulty understanding one part of the code below.我很难理解下面代码的一部分。 I was able to get it correct in an online lesson, so I'm not looking for help finishing it, just understanding this: in the last segment of code, I'm confused about how the x and food work.我能够在在线课程中正确理解它,所以我不是在寻求帮助来完成它,只是理解这一点:在最后一段代码中,我对x和food的工作方式感到困惑。 How is the x going through the items in prices? x 如何处理价格中的项目?
shopping_list = ["banana", "orange", "apple"]
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
def compute_bill(food):
total = 0
for x in food:
total = total + prices[x]
return total
Python dictionaries
(and other python data structures) implement what is called an iterator
pattern , which takes one item at a time, in order, untill it traverses the whole data structure. Python dictionaries
(和其他 python 数据结构)实现了所谓的iterator
模式,它按顺序一次取一个项目,直到遍历整个数据结构。
Dictionaries implement a tp_iter slot that returns an efficient iterator that iterates over the keys of the dictionary.字典实现了一个 tp_iter 槽,它返回一个高效的迭代器,该迭代器对字典的键进行迭代。 During such an iteration, the dictionary should not be modified, except that setting the value for an existing key is allowed (deletions or additions are not, nor is the update() method).在这样的迭代期间,不应修改字典,除非允许为现有键设置值(删除或添加不允许,update() 方法也不允许)。 This means that we can write这意味着我们可以写
for k in dict: ...
which is equivalent to, but much faster than这相当于,但比
for k in dict.keys(): ...
as long as the restriction on modifications to the dictionary (either by the loop or by another thread) are not violated.只要不违反对字典修改的限制(通过循环或另一个线程)。
The code: for x in food:
simply inits the iterator
in the python dict
and calls it repeatedly to get the next item untill the last item.代码: for x in food:
只需在 python dict
中初始化iterator
并重复调用它以获取下一项直到最后一项。
That is how it works in python (and other languages as well).这就是它在 python(以及其他语言)中的工作方式。 Python knows internaly that the dict
implements an iterator
and for
loops call this iterator underneath. Python 内部知道dict
实现了一个iterator
, for
循环在下面调用这个迭代器。
prices
is a dictionary, a mapping of keys (banana, apple, ...) to values (4, 2, ...). prices
是一个字典,是键(香蕉、苹果、...)到值(4、2、...)的映射。
for x in food
means "For every item in the provided list called food
, give it a temporary variable assignment x
and do something with x
." for x in food
意思是“对于提供的名为food
的列表中的每个项目,给它一个临时变量赋值x
并用x
做一些事情。”
total = total + prices[x]
means "Assign the current value of total
added to the price of item x
(looked up from prices)". total = total + prices[x]
表示“将total
的当前值分配给项目x
的价格(从价格中查找)”。 For the first item in food
(banana in this case), you're looking for a corresponding price in prices
(which is 4).对于food
中的第一项(在本例中为香蕉),您正在寻找相应的价格prices
(即 4)。 So, you're really saying total = 0 + 4
, then moving onto the next item in food
.因此,您实际上是在说total = 0 + 4
,然后转到food
中的下一项。 Since total
is now set to 4, your assignment becomes total = 4 + price['orange']
, or 4 + 1.5
.由于total
现在设置为 4,因此您的分配变为total = 4 + price['orange']
或4 + 1.5
。 Once the list has been iterated entirely, you have a total
sum (7.5 in your example).列表完全迭代后,您就有了一个total
(在您的示例中为 7.5)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.