从字典中的itemgetter查找值 [英] Lookup values from itemgetter in a dict

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

问题描述

我有一个要排序的元组列表。元组包含字符串:

  connectionsList = [('C','B'),('A','C'),(' D','B'),('C','D')] 

元组中的字符串为数字存储在字典中的值:

  valuesDict = {'A':3,'B':5,'C':1,'D' :2} 

我要执行的操作是按照字典中dict值的总和对列表进行排序元组。此玩具示例的所需输出为:

  [(C,D),(A,C),(C,B),(D ,B)] 

总和为:

  [3、4、6、7] 

使用itemgetter,我可以按位置的字母顺序排列:

  sortedView = sorted(connectionsList,key = itemgetter(0,1))

但是,我在 dict itemgetter 中查找值时遇到麻烦c>。运行此命令:

  sortedView = sorted(connectionsList,key = valuesDict [itemgetter(0)] + valuesDict [itemgetter(1)])

给出dict错误 KeyError:operator.itemgetter(0)。 / p>

如何基于 dict 中的值创建排序?

解决方案

不要使用 itemgetter 实例;您需要称呼它们,但它们在这里是过大的。



只需将 key 值传递给 lambda 。这是一个元组,因此添加索引:

  sortedView = sorted(connectionsList,key = lambda k:valuesDict [k [0] ] + valuesDict [k [1]])

参数必须是一个可调用对象,它接受一个参数(正在排序的项之一),并返回您实际在其上进行排序的值。 itemgetter()返回基于对您调用对象的索引的结果; itemgetter(0)(some_tuple)将返回传入的元组的第一个元素。



lambda可以做到同样,在上述解决方案中,lambda返回给定元组的所需总和。



演示:

 >> connectionsList = [('C','B'),('A','C'),('D','B'),('C','D')] 
>> ;> valuesDict = {'A':3,'B':5,'C':1,'D':2}
>>> sorted(connectionsList,key = lambda k:valuesDict [k [0]] + valuesDict [k [1]])
[('C','D'),('A','C'), ('C','B'),('D','B')]

您也可以将您的元组视为任意长度的序列。如果然后还考虑了以下可能性,即元组中的所有值都不都是 valuesDict 映射的有效键(采用 0 代替),您还可以使用以下方法得到解决方案:

  sortedView = sorted(connectionsList,key = lambda k:sum(valuesDict .get(v,0)for v in k))

这是更通用且更可靠的。 / p>

您也不需要按字母顺序使用 itemgetter()对象;元组已经以 0,1 的顺序提供,因此您将使用排序键获得相同的排序顺序而无需


I have a list of tuples that I'm trying to sort. The tuples contain strings:

connectionsList = [('C', 'B'), ('A', 'C'), ('D', 'B'), ('C','D')]

The strings in the tuples have numeric values stored in a dict:

valuesDict = {'A':3, 'B':5, 'C':1, 'D':2}

What I'd like to do is sort the list by the sum of the values in the dict for the tuples. Desired output for this toy example would be:

[(C,D), (A,C), (C,B), (D,B)]

which would have the sums of:

[3, 4, 6, 7]

Using itemgetter, I can sort alphabetically by position:

sortedView = sorted(connectionsList, key=itemgetter(0,1))

However, I'm having trouble looking up the values from itemgetter in the dict. Running this:

sortedView = sorted(connectionsList, key=valuesDict[itemgetter(0)] + valuesDict[itemgetter(1)])

gives a dict error of KeyError: operator.itemgetter(0).

How do I create a sort based on the values in the dict?

解决方案

Don't use itemgetter instances; you need to call them but they are overkill here.

Just access the dictionary directly with the key values passed into a lambda. It is a tuple, so add indexing:

sortedView = sorted(connectionsList, key=lambda k: valuesDict[k[0]] + valuesDict[k[1]])

The key argument must be a callable object, that takes a single argument (one of the items being sorted), and returns the value you are actually sorting on. itemgetter() that returns results based on indexing on the object you call it with; itemgetter(0)(some_tuple) would return the first element from the tuple passed in.

A lambda can do the same, in the above solution the lambda returns your desired sum for the given tuple.

Demo:

>>> connectionsList = [('C', 'B'), ('A', 'C'), ('D', 'B'), ('C','D')]
>>> valuesDict = {'A':3, 'B':5, 'C':1, 'D':2}
>>> sorted(connectionsList, key=lambda k: valuesDict[k[0]] + valuesDict[k[1]])
[('C', 'D'), ('A', 'C'), ('C', 'B'), ('D', 'B')]

You could also treat your tuple as an arbitrary-length sequence. If you then also account for the possibility that not all values in the tuples are valid keys for the valuesDict mapping (taking 0 instead), you could also get your solution with:

sortedView = sorted(connectionsList, key=lambda k: sum(valuesDict.get(v, 0) for v in k))

This is more general and robust.

You didn't need to use an itemgetter() object in your alphabetical sort either; the tuples are already provided in 0, 1 order, so you'd get the same sorting order without using a sort key.

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

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