首页 > 解决方案 > 在python中不使用for语句打印类列表的所有变量

问题描述

我在 list 中放了一些 classes( wow) a。我想在不使用 for 语句的情况下打印num所有元素的变量。a我应该怎么办?

我想(例如):

[1,5,3,4,0] # Expected output

我试过的:

import random as r

class wow():
    def __init__(self):
        self.num=r.randint(0,10)
a=[]
for x in range(5):
    a.append(wow())

print((lambda x: x)(a).num)

标签: pythonclass

解决方案


print(list(map(lambda x: x.num, a)))

推荐阅读