首页 \ 问答 \ 对字典中的值进行排序以检索每个键的最大值(Sorting Values in a dictionary to retrieve the maximum value for each key)

对字典中的值进行排序以检索每个键的最大值(Sorting Values in a dictionary to retrieve the maximum value for each key)

例如:我的字典如下:

dic = {'bob':['0,5,1'], 'annie':['2,4,0'], 'charles':['0,7,0']}

我希望能够:

首先:对键进行排序,使其按字母顺序排列

其次:对每个键的值进行排序,以便选择最大值

我期望的结果是:

annie:4
bob:5
charles:7

我已经在这很长一段时间了,并且只能打印出总额最大的整个列表。 例如,使用我当前的代码,我得到了这个:

[2, 4, 0]
[2, 4, 0]
[2, 4, 0]

这是因为程序找到具有最大总和的列表,并为字典中的每个键打印出来。 我已经使用了max(dic.values())

我非常感谢您的帮助和最有效的方法。 谢谢


For example: My dictionary is as follows:

dic = {'bob':['0,5,1'], 'annie':['2,4,0'], 'charles':['0,7,0']}

I want to be able to:

Firstly: Sort the keys so that they are arranged alphabetically

Secondly: Sort the values for each key so that the max value is selected

What I would expect as my outcome is:

annie:4
bob:5
charles:7

I have been at this for a very long time and have only been able to print out the whole list which has the greatest sum. For example with my current code I get this:

[2, 4, 0]
[2, 4, 0]
[2, 4, 0]

This is because the program finds the list with the greatest sum and prints that out for each key in the dictionary. I have used max(dic.values()) for this.

I would really appreciate your help and most efficient methods. Thank You


原文:https://stackoverflow.com/questions/29462923
更新时间:2023-02-27 08:02

最满意答案

<div class="cycle-slideshow"

http://jsfiddle.net/ska2hjr0/

根据文件:

cycle-slideshow是一个特殊的类名,它告诉Cycle2在加载页面时自动初始化幻灯片。

http://jquery.malsup.com/cycle2/demo/basic.php

另一种方法是,向页面添加jquery代码: $( '.slideshow' ).cycle(); 但是自动初始化甚至更好。


<div class="cycle-slideshow"

http://jsfiddle.net/ska2hjr0/

According to documentation:

cycle-slideshow is a special classname which tells Cycle2 to auto-initialize the slideshow when the page is loaded.

http://jquery.malsup.com/cycle2/demo/basic.php

Another method, add jquery code to page: $( '.slideshow' ).cycle(); But auto initialization is even better.

相关问答

更多
  • 如果图像父项设置为display:none,则resizeToParent插件将不起作用。 因为resizeToParent插件等待缓存图像,所以在大多数幻灯片已经设置为display:none之后才会运行。 在运行resizeToParent之后使用$('。slideshow')。cycle()以编程方式启动幻灯片,并且所有内容似乎都按预期工作。 function cropSlideImage() { $('.slideshow img').resizeToParent(); } $(docum ...
  • 你将不得不做的事是编辑jquery.cycle2.carousel.js文件,并更改转换函数。 我对偏移进行了硬编码,但是如果你愿意的话,你也许可以将它编码为基于你提供的百分比。 主要变化如下: var offset = 2; //Set the offset of your carousel, for 5 it will be 2. //Use this offset to calculate the max and min slide locations. var maxCurr = opts.slid ...
  • 加载循环插件后,在加载autoheight插件之前,您可以设置默认值 $.fn.cycle.defaults.autoHeightSpeed = 432432; After loading the cycle plugin and before loading the autoheight plugin you could set the defaults $.fn.cycle.defaults.autoHeightSpeed = 432432;
  • 这很简单......只需用Cycle2源代码中的cycle2替换“循环”,就可以使用它 $(something).cycle2({options...}) 就这么简单 - 我不明白他们为什么没有在网站上使用cycle2命名空间版本来实现旧的循环兼容性。 It's easy... just replacing the "cycle"s with cycle2 in the Cycle2 source code, makes it usable with $(something).cycle2({option ...
  • 你确定你已经包含了Cycle2的js和css文件吗? 而且你正在使用现有的图像和正确的路径? 您发布的代码没有任何问题,您可以在此处查看: http : //jsfiddle.net/natnaydenova/x4dfv/ 在你的HEAD部分的某个地方: