列表的字典中的Python miminum值 [英] Python miminum value in dictionary of lists

查看:160
本文介绍了列表的字典中的Python miminum值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉关于问题转贴...我应该刚刚编辑了这个问题。标记了新的mods。对不起,麻烦



由于需求有所改变,不得不重写该问题。



我有一本字典例如:

  d = {'a':[4,2],'b':[3,4 ],'c':[4,3],'d':[4,3],'e':[4],'f':[4],'g':[4]} 

我想获得与字典d中最小长度相关联的键,以及具有最大值



在这种情况下,长度最小的键(该字典中列表的最小长度)应返回

 'e,'f','g'

最有价值的人(每个列表中的整数的总和)应该返回

 'b''' 

我尝试过

  min_value = min(dict.itervalues())
min_keys = [k for d in如果dict [k] == min_value]
pre>

但是这并不能给我我想要的结果。



任何想法?



谢谢!

解决方案

如何使用排序和lambdas?

 #!/ usr / bin / env python 
d = {'a':['1'],'b':['1','2'],'c' :['8','1'],'d':['1'],'e':['1','2','3'],'f':[4,1]}
sorted_by_sum_d = sorted(d,key = lambda key:sum(list(int(item)for d in [key])))
sorted_by_length_d = sorted(d,key = lambda key:len [key]))
print按列表中的项目的总和排序:%s%sorted_by_sum_d
print按列表中的项目长度排序:%s%sorted_by_length_d

这将输出:

 按列表中的项目的总和排序:['a','d','b','f','e','c'] 
按项目长度列表:['a','d','c','b','f','e']

请注意,我更改了初始的'd'只是为了确保它的工作)



然后,如果你想要的项目最大的总和,你得到最后一个元素的 sorted_by_sum_d 列表。



(我不太确定这是你想要的,但是)



修改



如果您可以确保列表总是将是整数列表(或数字类型,就此而言) ,例如 long float ...),则不需要将字符串转换为整数。 sorted_by_sum_d 变量的计算可以简单地使用:

  d = {'a':[1],'b':[1,2],'c':[8,1],'d':[1],'e':[1,2,3] 'f':[4,1]} 
sorted_by_sum_d = sorted(d,key = lambda key:sum(d [key]))


Sorry about the question repost...I should have just edited this question in the first place. Flagged the new one for the mods. Sorry for the trouble

Had to re-write the question due to changed requirements.

I have a dictionary such as the following:

d = {'a': [4, 2], 'b': [3, 4], 'c': [4, 3], 'd': [4, 3], 'e': [4], 'f': [4], 'g': [4]}

I want to get the keys that are associated with the smallest length in the dictionary d, as well as those that have the maximum value.

In this case, the keys with the smallest length (smallest length of lists in this dictionary) should return

'e, 'f', 'g'

And those with the greatest value(the sum of the integers in each list) should return

'b' 'c'

I have tried

min_value = min(dict.itervalues())
min_keys = [k for k in d if dict[k] == min_value]

But that does not give me the result I want.

Any ideas?

Thanks!

解决方案

How about using sorting and lambdas?

#!/usr/bin/env python
d = {'a': ['1'], 'b': ['1', '2'], 'c': ['8', '1'], 'd':['1'], 'e':['1', '2', '3'], 'f': [4, 1]}
sorted_by_sum_d = sorted(d, key=lambda key: sum(list(int(item) for item in d[key])))
sorted_by_length_d = sorted(d, key=lambda key: len(d[key]))
print "Sorted by sum of the items in the list : %s" % sorted_by_sum_d
print "Sorted by length of the items in the list : %s" % sorted_by_length_d

This would output:

Sorted by sum of the items in the list : ['a', 'd', 'b', 'f', 'e', 'c']
Sorted by length of the items in the list : ['a', 'd', 'c', 'b', 'f', 'e']

Be aware I changed the initial 'd' dictionary (just to make sure it was working)

Then, if you want the item with the biggest sum, you get the last element of the sorted_by_sum_d list.

(I'm not too sure this is what you want, though)

Edit:

If you can ensure that the lists are always going to be lists of integers (or numeric types, for that matter, such as long, float...), there's not need to cast strings to integers. The calculation of the sorted_by_sum_d variable can be done simply using:

d = {'a': [1], 'b': [1, 2], 'c': [8, 1], 'd':[1], 'e':[1, 2, 3], 'f': [4, 1]}
sorted_by_sum_d = sorted(d, key=lambda key: sum(d[key]))

这篇关于列表的字典中的Python miminum值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆