将具有两个值的 returns class 的生成器 pythonicly 转换为字典?
Transform generator that returns class with two values into dictionary pythonicly?
我有一个生成器,它接受字典并生成一个包含键和值的 class 和 returns。我想从中重新创建字典。
class Limits:
self._info = {"Test":"Toast"}
@property
def absolute(self):
for (name, value) in self._info['absolute'].items():
yield AbsoluteLimit(name, value)
class AbsoluteLimit(object):
def __init__(self, name, value):
self.name = name
self.value = value
我的肮脏解决方案是:
limits_dict = {}
for elem in Limits.absolute:
limits_dict[elem.name]=elem.value
是否有更 pythonic 的方式来创建字典?
有问题的方法来自https://github.com/openstack/python-novaclient/blob/master/novaclient/v2/limits.py
limits_dict = {elem.name: elem.value for elem in Limits.absolute}
我有一个生成器,它接受字典并生成一个包含键和值的 class 和 returns。我想从中重新创建字典。
class Limits:
self._info = {"Test":"Toast"}
@property
def absolute(self):
for (name, value) in self._info['absolute'].items():
yield AbsoluteLimit(name, value)
class AbsoluteLimit(object):
def __init__(self, name, value):
self.name = name
self.value = value
我的肮脏解决方案是:
limits_dict = {}
for elem in Limits.absolute:
limits_dict[elem.name]=elem.value
是否有更 pythonic 的方式来创建字典?
有问题的方法来自https://github.com/openstack/python-novaclient/blob/master/novaclient/v2/limits.py
limits_dict = {elem.name: elem.value for elem in Limits.absolute}