来自Dict的itemgetter的查找值(python) [英] lookup values from itemgetter in a Dict (python)

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

问题描述

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

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}

我想做的是按照元组dict中的值之和对列表进行排序.此玩具示例的所需输出为:

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

其总和为:

[3, 4, 6, 7]

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

Using itemgetter, I can sort alphabetically by position:

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

但是,我在字典中查找itemgetter的值时遇到了麻烦.运行此:

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

给出dict错误 KeyError:operator.itemgetter(0).

如何根据字典中的值创建排序?

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

推荐答案

不要使用itemgetter实例;您需要给他们打电话,但他们在这里过分杀人了.

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

只需将传递给lambdakey值直接访问字典.这是一个元组,因此添加索引:

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

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

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.

lambda可以执行相同的操作,在上述解决方案中,lambda返回给定元组所需的总和.

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

演示:

>>> 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),则也可以使用以下方法获得解决方案:

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

这更通用,更强大.

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

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.

这篇关于来自Dict的itemgetter的查找值(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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