python - 在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)
解决方案
print(list(map(lambda x: x.num, a)))
推荐阅读
- go - 创建嵌套目录时 os.MkdirAll() 失败
- javascript - 如何用 Jest 将两个参数测试为数字
- cypress - Cypress:如何在悬停时定位元素?
- caching - 为什么使用 PIVT 的缓存会遇到缓存别名问题?
- java - 无法使用数据库 org.hibernate.AnnotationException 自动生成的实体创建索引:无法创建索引
- c++ - 使用 CLRS 方法实现红黑树。无法发现我的错误
- three.js - WebGL:INVALID_VALUE:texImage2D:three.js 中没有画布
- c++ - 为什么 std::tie 不能与 const 类方法一起使用?
- java - 如何编写带有字母和数字的转换(小写->大写,个位数->其值的两倍,两位数->其数字的总和)程序?
- xslt - XSLT:尝试根据这两个节点的元素值组合两个节点