4 回答
TA贡献1873条经验 获得超9个赞
我建议你使用itertools.chain.from_iterable
来展平中间列表
In [6]: import itertools
In [7]: print(list(itertools.chain.from_iterable([[foo.a, foo.b] for foo in data])))
[1, 2, 3, 4]
TA贡献1828条经验 获得超13个赞
from functools import reduce
from dataclasses import dataclass
@dataclass
class Foo:
a: int
b: int
data = [
Foo(1,2),
Foo(3,4)
]
result = [[foo.a, foo.b] for foo in data]
print(reduce(list.__add__, result))
TA贡献1875条经验 获得超3个赞
你可以这样做:
print([value for foo in data for attr, value in foo.__dict__.items()])
它基本上是在做两个 for 循环,与做的一样:
for foo in data:
for attr, value in foo.__dict__.items():
print(value)
添加回答
举报