理解Python字典上的max函数操作(Understanding the max function operation on Python dictionary)
我试图理解Python字典上max函数的操作。 以下是我使用的代码:
tall_buildings = { "Empire State": 381, "Sears Tower": 442, "Burj Khalifa": 828, "Taipei 101": 509 } # 1. find the height of the tallest building print("Height of the tallest building: ", max(tall_buildings.values())) # 2. find the name, height pair that is tallest print(max(tall_buildings.items(), key=lambda b: b[1])) # 3. find the tallest building print(max(tall_buildings, key=tall_buildings.get))
所有上述打印语句都给出了正确的结果,如代码中的注释所示。
我理解
#1
和#2
是如何工作的。1:
tall_buildings.values()
给出一个高度流,max
函数返回高度的最大值。2:
tall_buildings.items()
给出(名称,高度)对的流,并且max函数根据key=pair's height.
返回该对key=pair's height.
但是,我很难理解
# 3
是如何工作的。key=tall_buildings.get
如何成为寻找最高建筑的关键?我从Ned的Pycon Talk中获取了代码: https ://youtu.be/EnSu9hHGq5o ? t = 12m42s
I am trying to understand the operation on max function on Python dictionary. Following is the code I am using:
tall_buildings = { "Empire State": 381, "Sears Tower": 442, "Burj Khalifa": 828, "Taipei 101": 509 } # 1. find the height of the tallest building print("Height of the tallest building: ", max(tall_buildings.values())) # 2. find the name, height pair that is tallest print(max(tall_buildings.items(), key=lambda b: b[1])) # 3. find the tallest building print(max(tall_buildings, key=tall_buildings.get))
All the above print statements gives the correct results as indicated in the comments in the code.
I understood how
#1
and#2
works.1:
tall_buildings.values()
gives a stream of heights andmax
function returns the max of the heights.2:
tall_buildings.items()
gives a stream of (name, height) pairs and max function returns the pair based on thekey=pair's height.
However, I have difficulty in understanding how
# 3
works. How doeskey=tall_buildings.get
serve as the key for finding the tallest building ?I have taken the code from Ned's Pycon Talk: https://youtu.be/EnSu9hHGq5o?t=12m42s
原文:https://stackoverflow.com/questions/44566537
最满意答案
为什么不在你要停止的帧处使用javascript的
setTimeout
函数?... if (iteration < totalIterations) { iteration++; } else { iteration = 0; moveRight = !moveRight; } if (iteration == 0 && moveRight){ setTimeout(function(){ requestAnimationFrame(drawCircle); }, 3000); }else{ requestAnimationFrame(drawCircle); } ...
如果你害怕
setTimeout
函数的不准确,另一个选项是:if (iteration < totalIterations) { iteration++; } else { iteration = 0; moveRight = !moveRight; } if (iteration == 0 && moveRight){ var next_iter = new Date().getTime() + 3000; delay_restart(next_iter); }else{ requestAnimationFrame(drawCircle); } } drawCircle(); function delay_restart(start) { while (new Date().getTime() < start ){ } requestAnimationFrame(drawCircle) }
Why not use javascript's
setTimeout
function at the frame you want to stop?... if (iteration < totalIterations) { iteration++; } else { iteration = 0; moveRight = !moveRight; } if (iteration == 0 && moveRight){ setTimeout(function(){ requestAnimationFrame(drawCircle); }, 3000); }else{ requestAnimationFrame(drawCircle); } ...
another option in case you're scared of the inaccuracy of the
setTimeout
function:if (iteration < totalIterations) { iteration++; } else { iteration = 0; moveRight = !moveRight; } if (iteration == 0 && moveRight){ var next_iter = new Date().getTime() + 3000; delay_restart(next_iter); }else{ requestAnimationFrame(drawCircle); } } drawCircle(); function delay_restart(start) { while (new Date().getTime() < start ){ } requestAnimationFrame(drawCircle) }
相关问答
更多-
在html画布上下雨动画(rain drop animation on html canvas)[2022-07-22]
以下是我将如何做你想做的事情: function makeit() { // These variables can be used in the drawing functions var canvas = document.getElementById("mycanvas"); var ctx = canvas.getContext('2d'); var drops = []; // Init canvas & drops init(); // ... -
在脚本底部调用draw() ,将其替换为: // Note: no parens in the draw window.setTimeout(draw,3000); At the bottom of your script where you call draw(), replace that with: // Note: no parens in the draw window.setTimeout(draw,3000);
-
Felix Kling是对的,你应该将你的绘图包装在一个onload回调中。 请看下面的代码。 jsFiddle到它就在这里 。 (我已经删除了requestAnimationFrame以使代码更简单,但我认为这不是问题。) $(function () { function drawFrame() { //window.requestAnimationFrame(drawFrame, canvas); var c = document.getElementByI ...
-
所以问题来自于你对性能的使用。现在并不总是如此,特别是在精确定时器功耗过高的移动设备上。 只需使用requestAnimationFrame提供的时间:在准确的浏览器/设备上,它将使用亚毫秒精度,否则它将只有毫秒精度。 (准确= Chrome桌面肯定,......其他???) 我将让你看到我如何使用rAF的时间来构建当前'dt'=自上一帧以来经过的时间,'applicationTime'=应用程序中经过的时间( 不计算你选择应用程序时)。 此方法的第二个好处是,您可以轻松地将应用程序速度更改为“子弹时间”或 ...
-
我已经发表评论,向您展示现有的变化。 我觉得我不需要用很少的变化来解释,但我添加了评论以指出它们。 附加:自动对焦和对焦轮廓删除( 请参阅源代码中的注释 ) var main = function (){ var canvas = document.getElementById("canvas"); canvas.addEventListener("keydown", doKeyDown, true); var ctx = canvas.getContext("2d"); ctx.fillStyle ...
-
在javascript中,您可以使用setInterval和setTimeout函数来创建延迟并限制帧速率。 例如,如果你想让你的绘图循环大约30 FPS,你可以有一些看起来像这样的代码: function draw(){ var canvas = document.getElementById('myCanvas'); //create the image object var img = new Image(); //set the im ...
-
HTML5 Canvas动画问题(HTML5 Canvas animation issue)[2022-01-23]
两种解决方案 在将动画移动到新位置之前,重新绘制动画顶部的背景。 这种所谓的脏精灵技术 - 更快 - 更复杂。 在帧之间重绘整个画布 如果绘制背景是一个复杂的操作,只需将准备好的背景缓冲在另一个画布中以提高速度。 Two solutions Redraw the background on the top of animation before moving it to the new location. This so called dirty sprite technique - faster - mo ... -
HTML Canvas:动画延迟(HTML Canvas: Animation Delay)[2023-07-03]
动画列表/堆栈。 关键帧 最好的方法是设置代码来管理关键帧,这样您就可以为希望随时间变化的对象的每个属性创建一个关键帧列表。 这允许您创建可以序列化为JSON文件的非常复杂的动画。 这将动画与代码分离,但需要更多代码。 动画列表 如果它只是一个简单的动画,那么您可以创建一个动画堆栈(如果动画顺序是静态的,则列表),这只是一组为动画的每个部分依次调用的函数。 您为动画的每个部分设置了一个startTime ,小心地将其设置为一致,这样您就不会有任何时间漂移。 如果动画周期为4秒,那么它应该每4秒重复一次,在4 ... -
HTML Canvas:延迟动画(HTML Canvas: Delay Animation)[2024-03-17]
为什么不在你要停止的帧处使用javascript的setTimeout函数? ... if (iteration < totalIterations) { iteration++; } else { iteration = 0; moveRight = !moveRight; } if (iteration == 0 && moveRight){ setTimeout(function(){ requestAnimationFram ... -
画布和动画(Canvas and animation)[2021-07-30]
你没有任何检查,看你是否已经绘制,所以这里是你的代码与这些更改评论,以及真正的像素位置修复( http://jsfiddle.net/upgradellc/ htJXy / 1 / ): $(document).ready(function(){ var x, y, context, painter, canvas; var xCounter = 0 , yCounter = 0; var xarray = []; var yarray = []; functio ...