如何将命名元组转换为值列表并保持属性顺序?
from collections import namedtuple
Gaga = namedtuple('Gaga', ['id', 'subject', 'recipient'])
g = Gaga(id=1, subject='hello', recipient='Janitor')
我想得到这个列表(要保持属性的顺序):
[1, 'hello', 'Janitor']
我可以手动自己创建这个列表,但肯定有更简单的方法。 我试过:
g._asdict().values()
但是属性的顺序不是我想要的。
1 个回答
63
为什么不直接用 list
呢?
>>> list(g)
[1, 'hello', 'Janitor']