作为学习的一部分,我必须使用append函数添加:
video_ad_1 = {"title": "Healthy Living", "company": "Health Promotion Board", "views": 15934}
video_ad_2 = {"title": "Get a ride, anytime anywhere", "company": "Uber", "views": 923834}
video_ad_3 = {"title": "Send money to your friends with GrabPay", "company": "Grab", "views": 23466}
video_ad_4 = {"title": "Ubereats now delivers nationwide", "company": "Uber", "views": 1337}
video_ad_5 = {"title": "Get cabs now with UberFlash", "company": "Uber", "views": 90234}
进入此列表:
video_ads_list =[]
预期输出应为:
[{'title': 'Healthy Living', 'company': 'Health Promotion Board','views': 15934},
{'title': 'Get a ride, anytime anywhere', 'company': 'Uber', 'views': 923834},
.
.
{'title': 'Get cabs now with UberFlash', 'company': 'Uber', 'views': 90234}]
该问题指出我需要使用append函数。
答案 0 :(得分:0)
google是我们最好的朋友,伙计。根据{{3}}
list.append(elem)-在列表末尾添加一个元素。
video_ads_list.append(video_ad_1)
在链接中,您可以找到许多其他有用的列表方法。尝试和他们一起玩。那就是学习东西的方式
答案 1 :(得分:0)
您可以在循环中使用append函数。一个单一的解决方案是使用extend
video_ad_1 = {"title": "Healthy Living", "company": "Health Promotion Board",
"views": 15934}
video_ad_2 = {"title": "Get a ride, anytime anywhere", "company": "Uber", "views":
923834}
video_ad_3 = {"title": "Send money to your friends with GrabPay", "company": "Grab",
"views": 23466}
video_ad_4 = {"title": "Ubereats now delivers nationwide", "company": "Uber",
"views": 1337}
video_ad_5 = {"title": "Get cabs now with UberFlash", "company": "Uber", "views":
90234}
video_ads_list =[]
video_ads_list.extend((video_ad_1, video_ad_2, video_ad_3, video_ad_4, video_ad_5))
答案 2 :(得分:0)
您可以创建一个列表:
video_ads_list=[video_ad_1,video_ad_2,video_ad_3,video_ad_4,video_ad_5]
还是最好的:
video_ads_list=[globals()['video_ad_%d'%i] for i in range(1,6)]