I put some classes(wow) in list a.
And I want to print the variable num of all elements of a without using for statement.
What should I do?
I want to(example):
[1,5,3,4,0] # Expected output
What I have tried:
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)
You can add __str__() method to your class:
class wow():
def __init__(self):
self.num=r.randint(0,10)
def __str__(self):
return str(self.num)
then by just printing:
print(*a)
you will get:
3 9 4 9 3
in addition, reading this Link might be good for better clue:
print(list(map(lambda x: x.num, a)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With