简体   繁体   English

该Python代码如何正常工作?

[英]How is it that this Python code works correctly?

This piece of Python code prints out the maximum age in the Age = {age:numberOfOccurence} dictionary. 这段Python代码会在Age = {age:numberOfOccurence}字典中打印出最大年龄。 But because all the ages are already greater than zero, why does the code print out 50 as the maximum age? 但是因为所有年龄段都已经大于零,所以为什么代码会打印出50个最大年龄段?

Age = {45:3, 50:2, 25:1, 10:5, 15:10}
Oldest = 0

for MaxAge in Age:
    if MaxAge>Oldest:
        Oldest = MaxAge

print(Oldest)

I commented the code and added some print statements to help you understand how it works. 我对代码进行了注释,并添加了一些打印语句,以帮助您了解其工作方式。

Age = {45:3, 50:2, 25:1, 10:5, 15:10}
Oldest = 0 ## Initialize oldest with the minimum number

print('Initial Oldest Value: ', Oldest)

for MaxAge in Age: ## Iterating Age
    ## If age at current index is greater than oldest (which is 0 for first iteration)  
    if MaxAge>Oldest: 
        ## Update oldest with current age
        print('MaxAge: ', MaxAge, ' > Oldest: ', Oldest)
        Oldest = MaxAge
        print('New Oldest Value: ', Oldest)
        print('-'*10)


print('Final Oldest Value: ', Oldest)

Output 输出量

Initial Oldest Value:  0                                                                                                         
MaxAge:  25  > Oldest:  0                                                                                                        
New Oldest Value:  25                                                                                                            
----------                                                                                                                       
MaxAge:  50  > Oldest:  25                                                                                                       
New Oldest Value:  50                                                                                                            
----------                                                                                                                       
Final Oldest Value:  50  

Because, every-time something is greater than zero, you make the code set the Oldest variable to that, then next is greater than previous, that will be assigned, and so on... all this are done from this if statement: 因为每次某事大于零,您都将代码设置为Oldest变量,然后将下一个大于前一个变量,然后将其赋值,依此类推……所有这些都是通过以下if语句完成的:

if MaxAge>Oldest:
    Oldest = MaxAge

If you take out that, and add something else for the for loop, it won't work as expected. 如果将其删除,然后为for循环添加其他内容,它将无法按预期工作。

Note that @andreihondrari's comment. 请注意,@ andreihondrari的评论。

I'm not sure if you are expecting the code to do something else, or if it's that you don't understand why it works. 我不确定您是否期望代码执行其他操作,或者您是否不知道它为什么起作用。

However, you could take a look at this question and answer: Iterating over dictionaries using 'for' loops 但是,您可以看一下这个问题和答案: 使用“ for”循环遍历字典

The reason the code works is that the for loop over a dictionary will iterate over the keys of the dictionary. 代码起作用的原因是字典上的for循环将迭代字典的键。 You can see this if you add an extra print statement like this: 如果添加这样的额外打印语句,则可以看到以下内容:

Age = {45:3, 50:2, 25:1, 10:5, 15:10}
Oldest = 0

for MaxAge in Age:
    print(MaxAge)
    if MaxAge>Oldest:
        Oldest = MaxAge

print(Oldest)

Then you should see: 然后,您应该看到:

45
50
25
10
15
50

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

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