因此,我有一个包含嵌套字典的列表,而我想做的就是从字典中读取视图的值,然后将它们放入乘数。
video_ads=({'title': 'Healthy Living', 'company': 'Health Promotion
Board','views': 15934},
{'title': 'Get a ride, anytime anywhere', 'company': 'Uber', 'views':
923834},
{'title': 'Send money with GrabPay', 'company': 'Grab', 'views': 23466},
{'title': 'Ubereats now delivers nationwide', 'company': 'Uber', 'views':
1337},
{'title': 'Get cabs now with UberFlash', 'company': 'Uber', 'views': 90234})
所以我想做的就是获得标题以及因某种因素而产生的多种观点,这就是我到目前为止所写的内容
def payment_for_views(x):
for x in range(0,5):
x = x+1
if video_ads[x]['views'] >= 50000:
payment = video_ads[x]['views']*0.55
elif views <50000 and video_ads[x]['views'] >=10000:
payment=video_ads[x]['views']*0.68
else:
payment = video_ads[x]['views']*0.82
#print(x)
print(video_ads[x]['title'] + ': $'+ str(payment))
print(video_ads[x]['title'] + ': $'+ str(payment))
print(video_ads[x]['title'] + ': $'+ str(payment))
print(video_ads[x]['title'] + ': $'+ str(payment))
print(video_ads[x]['title'] + ': $'+ str(payment))
例如,似乎只打印一行:
Get cabs now with UberFlash: $49628.700000000004
Get cabs now with UberFlash: $49628.700000000004
Get cabs now with UberFlash: $49628.700000000004
Get cabs now with UberFlash: $49628.700000000004
Get cabs now with UberFlash: $49628.700000000004
这就是我所得到的。我需要5行所有的标题和数量。救命!!!并预先感谢!
答案 0 :(得分:1)
video_ads=({'title': 'Healthy Living', 'company': 'Health Promotion Board','views': 15934},
{'title': 'Get a ride, anytime anywhere', 'company': 'Uber', 'views': 923834},
{'title': 'Send money with GrabPay', 'company': 'Grab', 'views': 23466},
{'title': 'Ubereats now delivers nationwide', 'company': 'Uber', 'views': 1337},
{'title': 'Get cabs now with UberFlash', 'company': 'Uber', 'views': 90234})
def payment_for_views(item):
if item >= 50000:
payment = item * 0.55
elif item < 50000 and item >= 10000:
payment = item * 0.68
else:
payment = item * 0.82
return payment
[print(per_ad['title'], ': $' + str(payment_for_views(per_ad['views']))) for per_ad in video_ads]
输出:
健康生活:$ 10835.12
随时随地乘车:$ 508108.70000000007
使用GrabPay汇款:$ 15956.880000000001
Ubereats现在在全国范围内销售:1096.34美元
立即使用UberFlash出租车:$ 49628.700000000004
答案 1 :(得分:0)
for a in video_ads:
print('%s: $%.2f' % (a['title'], a['views'] * (0.55 if a['views'] >= 50000 else 0.68 if a['views'] >= 10000 else 0.82)))
这将输出:
Healthy Living: $10835.12
Get a ride, anytime anywhere: $508108.70
Send money with GrabPay: $15956.88
Ubereats now delivers nationwide: $1096.34
Get cabs now with UberFlash: $49628.70
答案 2 :(得分:0)
您需要将print
语句放入for
循环中。
在循环的前后留置print
意味着x
将固定为for
循环中设置的最终值。
def payment_for_views(x):
for x in range(len(x)):
if video_ads[x]['views'] >= 50000:
payment = video_ads[x]['views']*0.55
elif video_ads[x]['views'] <50000 and video_ads[x]['views'] >=10000:
payment=video_ads[x]['views']*0.68
else:
payment = video_ads[x]['views']*0.82
print(video_ads[x]['title'] + ': $'+ str(payment))
有多种方法可以改善代码:
这是一个演示:
def payment_for_views(x):
for item in x:
views = item['views']
if views >= 50000:
factor = 0.55
elif 10000 <= views < 50000:
factor = 0.68
else:
factor = 0.82
print(f'{item["title"]}: ${views*factor:.2f}')
payment_for_views(video_ads)
结果:
Healthy Living: $10835.12
Get a ride, anytime anywhere: $508108.70
Send money with GrabPay: $15956.88
Ubereats now delivers nationwide: $1096.34
Get cabs now with UberFlash: $49628.70