Python 将返回字典中的值分配给唯一变量

Python 将返回字典中的值分配给唯一变量,python,dictionary,Python,Dictionary,如果一个函数返回一个如上所示的字典,并且函数调用所需的时间比所需的时间长,那么将返回值分配给唯一变量的最佳方法是什么?例如,我的应用程序不关心胡萝卜色,也不知道如何实现foodColors() 我目前正在做显而易见的事情: def foodColors(): """Example function to return dictionary of food colors.""" appleColor = getAppleCol() # long magic function

如果一个函数返回一个如上所示的字典,并且函数调用所需的时间比所需的时间长,那么将返回值分配给唯一变量的最佳方法是什么?例如,我的应用程序不关心胡萝卜色,也不知道如何实现
foodColors()

我目前正在做显而易见的事情:

def foodColors():
    """Example function to return dictionary of food colors."""
    appleColor = getAppleCol() # long magic function
    return {'apple':appleColor, 'carrot':'orange', 'grape':'green'}
这种方法很好,但我希望有人能有更好的解决方案,或者能够展示一种独特的方式来内联解压返回值,例如:

colorDict = foodColors()
apple = colorDict['apple']
grape = colorDict['grape']

您可以使用
操作符.itemgetter

apple, grape = foodColors()['apple','grape'] # made up syntax
当然,如果需要,可以重复使用itemgetter函数:

apple, grape = itemgetter('apple', 'grape')(foodColors())

您可以使用
操作符.itemgetter

apple, grape = foodColors()['apple','grape'] # made up syntax
当然,如果需要,可以重复使用itemgetter函数:

apple, grape = itemgetter('apple', 'grape')(foodColors())
可能(取决于您如何使用dict)也是一种解决方案:

getter = itemgetter('apple', 'grape')
apple, grape = getter(foodColors())
apple2, grape2 = getter(foodColors())
优势

  • namedtuples
    非常轻量级
  • 漂亮的点式属性访问(
    obj.attr
缺点

  • 要像我在最后一行中显示的那样使用解包,您将始终需要解包所有值,如果您需要或不需要它们。也许这适用于您或其他人的用例,也许不适用
当然,如果
namedtuples
符合要求,您可以完全跳过中间字典


背景

这和你的玩具语法非常相似

from collections import namedtuple

color_dict = {'apple': 'red',
              'carrot':'orange',
              'grape':'green'}

# Create a namedtuple class
ColorMap = namedtuple('ColorMap', ['apple', 'carrot', 'grape'])

# Create an instance of ColorMap by using the unpacked dictionary
# as keyword arguments to the constructor
color_map = ColorMap(**color_dict)

# Unpack the namedtuple like you would a regular one
apple, carrot, grape = color_map
几乎能起作用:

apple, grape = foodColors()['apple','grape'] # made up syntax
(仅从dict中获取值列表,并将其像常规元组一样解压)。 问题是字典没有顺序(它们的键/值对的顺序是任意的。不是随机的(根本不是),而是任意的,顺序会改变)

但是,命名元组的字段是有序的(与常规元组一样,在
namedtuple
中,它们只是命名字段)。因此,在某种程度上,您可以获得字典以及元组等轻量级有序结构的一些好处。这就是为什么它们可以按顺序解包

但是,如果这样做,则依赖于元组中字段的确切顺序,因此放弃了它们提供的一个巨大优势。有关
namedtuples
的更多信息以及为什么它们很棒,请参阅Raymond Hettinger的文章。

可能(取决于您如何使用dict),也可能是一个解决方案:

getter = itemgetter('apple', 'grape')
apple, grape = getter(foodColors())
apple2, grape2 = getter(foodColors())
优势

  • namedtuples
    非常轻量级
  • 漂亮的点式属性访问(
    obj.attr
缺点

  • 要像我在最后一行中显示的那样使用解包,您将始终需要解包所有值,如果您需要或不需要它们。也许这适用于您或其他人的用例,也许不适用
当然,如果
namedtuples
符合要求,您可以完全跳过中间字典


背景

这和你的玩具语法非常相似

from collections import namedtuple

color_dict = {'apple': 'red',
              'carrot':'orange',
              'grape':'green'}

# Create a namedtuple class
ColorMap = namedtuple('ColorMap', ['apple', 'carrot', 'grape'])

# Create an instance of ColorMap by using the unpacked dictionary
# as keyword arguments to the constructor
color_map = ColorMap(**color_dict)

# Unpack the namedtuple like you would a regular one
apple, carrot, grape = color_map
几乎能起作用:

apple, grape = foodColors()['apple','grape'] # made up syntax
(仅从dict中获取值列表,并将其像常规元组一样解压)。 问题是字典没有顺序(它们的键/值对的顺序是任意的。不是随机的(根本不是),而是任意的,顺序会改变)

但是,命名元组的字段是有序的(与常规元组一样,在
namedtuple
中,它们只是命名字段)。因此,在某种程度上,您可以获得字典以及元组等轻量级有序结构的一些好处。这就是为什么它们可以按顺序解包


但是,如果这样做,则依赖于元组中字段的确切顺序,因此放弃了它们提供的一个巨大优势。有关
namedtuples
以及为什么它们很棒的更多信息,请参阅Raymond Hettinger。

Itemgetter在2.5+版本中只接受多个参数(我将这个问题标记为2.7,这个解决方案在那里运行得很好。在我的2.4环境中尝试它不起作用——尽管是我的错)。@Caprooja。python2.4比我的时代早了一点:)Itemgetter在版本2.5+中只接受多个参数(我将这个问题标记为2.7,这个解决方案在那里运行得很好。在我的2.4环境中尝试它没有效果——尽管是我的错)。@Caprooja——很公平。python2.4比我的时代早了一点:)