我有像这样的循环python
for i in view_overall_isp_ratings:
#the int and the round is just for casting the values returned
me = (int(round(i.avg_of_ratings)))
print(me)
and this prints intergers like this 1
1
1
1
4
我想要什么
for it to produce a list like this
[ 1 ,1 ,1 ,1 ,4]
尝试玩[],但至少我能得到的是
[1]
[1]
[1]
[1]
[4]
任何人都可以提供协助
答案 0 :(得分:1)
你需要创建一个列表(在循环之外!)并在每次循环迭代中追加它:
lst = []
for i in view_overall_isp_ratings:
#the int and the round is just for casting the values returned
lst.append(int(round(i.avg_of_ratings)))
print(lst)
或者,以更清洁的方式,您可以使用列表理解:
print([int(round(i.avg_of_ratings)) for i in view_overall_isp_ratings])