首页 \ 问答 \ 理解Python字典上的max函数操作(Understanding the max function operation on Python dictionary)

理解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 and max 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 the key=pair's height.

However, I have difficulty in understanding how # 3 works. How does key=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
更新时间:2022-05-21 12:05

最满意答案

为什么不在你要停止的帧处使用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)
}

codepen的第二个例子


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);
  }
...

codpen example

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)
}

codepen for second example

相关问答

更多
  • 以下是我将如何做你想做的事情: 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 ...
  • 两种解决方案 在将动画移动到新位置之前,重新绘制动画顶部的背景。 这种所谓的脏精灵技术 - 更快 - 更复杂。 在帧之间重绘整个画布 如果绘制背景是一个复杂的操作,只需将准备好的背景缓冲在另一个画布中以提高速度。 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 ...
  • 动画列表/堆栈。 关键帧 最好的方法是设置代码来管理关键帧,这样您就可以为希望随时间变化的对象的每个属性创建一个关键帧列表。 这允许您创建可以序列化为JSON文件的非常复杂的动画。 这将动画与代码分离,但需要更多代码。 动画列表 如果它只是一个简单的动画,那么您可以创建一个动画堆栈(如果动画顺序是静态的,则列表),这只是一组为动画的每个部分依次调用的函数。 您为动画的每个部分设置了一个startTime ,小心地将其设置为一致,这样您就不会有任何时间漂移。 如果动画周期为4秒,那么它应该每4秒重复一次,在4 ...
  • 为什么不在你要停止的帧处使用javascript的setTimeout函数? ... if (iteration < totalIterations) { iteration++; } else { iteration = 0; moveRight = !moveRight; } if (iteration == 0 && moveRight){ setTimeout(function(){ requestAnimationFram ...
  • 你没有任何检查,看你是否已经绘制,所以这里是你的代码与这些更改评论,以及真正的像素位置修复( 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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。