[英]Sum of values in a multiple lists of a dict
我是 python 的新手,并且一直在尝试添加通过迭代字典列表获得的值。
我一直遇到'builtin_function_or_method' object is not iterable' 错误消息或不受支持的类型。 任何帮助将非常感激。
这是我的代码:
def inventory(acct_info, months_subscribed, add_free_months, video_on_demand):
print(acct_info)
for info in acct_info:
print('-')
if info.get('months_subscribed') == 3:
months_subscribed_total = info.get('months_subscribed') * 18
elif info.get('months_subscribed') < 3:
months_subscribed_total = info['months_subscribed'] * 7
elif info.get('months_subscribed') > 3:
months_subscribed_total = info['months_subscribed'] - 3 * 7 + 18
print(f"User {info.get('name')} has months subscribed total of : $ {months_subscribed_total} ")
if info['ad_free_months'] > 0:
ad_free_total = info.get('ad_free_months') * 2
print(f" User {info.get('name')} total ad free is : {ad_free_total} ")
if info['video_on_demand'] > 0:
video_on_demand_total = info.get('video_on_demand') * 27.99
print(f" User {info.get('name')} total video on demand is : {video_on_demand_total} ")
acct_all_total = int(months_subscribed_total + ad_free_total + video_on_demand_total)
acct_all_total = [int(acct_all_total)]
print(f"Total for {info.get('name')} is: {acct_all_total} ")
acct_info = [{'name': 'acct_1', 'months_subscribed' : 2 , 'ad_free_months' : 3 , 'video_on_demand' : 1} ,
{'name': 'acct_2', 'months_subscribed' : 1 , 'ad_free_months' : 2 , 'video_on_demand' : 2},
{'name': 'acct_3', 'months_subscribed' : 2 , 'ad_free_months' : 1 , 'video_on_demand' : 3}]
combined_total = 0
months_subscribed = 0
ad_free_months = 0
video_on_demand = 0
months_subscribed_total = 0
ad_free_total = 0
video_on_demand_total = 0
inventory(acct_info, months_subscribed, ad_free_months, video_on_demand)
acct_all_total = 0
main()
Output 到目前为止是:
User acct_1 has months subscribed total of : $ 14
User acct_1 total ad free is : 6
User acct_1 total video on demand is : 27.99
Total for acct_1 is: [47]
-
User acct_2 has months subscribed total of : $ 7
User acct_2 total ad free is : 4
User acct_2 total video on demand is : 55.98
Total for acct_2 is: [66]
-
User acct_3 has months subscribed total of : $ 14
User acct_3 total ad free is : 2
User acct_3 total video on demand is : 83.97
Total for acct_3 is: [99]
我要总结的是所有用户的总数。 我设法得到每个用户的总数,但我想添加总数。 谢谢你。
我修改了您的库存 function,现在它在循环开始之前包含一个列表,它包含您在循环底部打印的所有用户总数,最后使用sum
function 所有用户的总数可以是计算出来的
def inventory(acct_info, months_subscribed, add_free_months, video_on_demand):
print(acct_info)
all_users_collection = []
for info in acct_info:
print('-')
if info.get('months_subscribed') == 3:
months_subscribed_total = info.get('months_subscribed') * 18
elif info.get('months_subscribed') < 3:
months_subscribed_total = info['months_subscribed'] * 7
elif info.get('months_subscribed') > 3:
months_subscribed_total = info['months_subscribed'] - 3 * 7 + 18
print(f"User {info.get('name')} has months subscribed total of : $ {months_subscribed_total} ")
if info['ad_free_months'] > 0:
ad_free_total = info.get('ad_free_months') * 2
print(f" User {info.get('name')} total ad free is : {ad_free_total} ")
if info['video_on_demand'] > 0:
video_on_demand_total = info.get('video_on_demand') * 27.99
print(f" User {info.get('name')} total video on demand is : {video_on_demand_total} ")
acct_all_total = int(months_subscribed_total + ad_free_total + video_on_demand_total)
acct_all_total = [int(acct_all_total)]
all_users_collection.append(int(acct_all_total))
print(f"Total for {info.get('name')} is: {acct_all_total} ")
acct_info = [{'name': 'acct_1', 'months_subscribed' : 2 , 'ad_free_months' : 3 , 'video_on_demand' : 1} ,
{'name': 'acct_2', 'months_subscribed' : 1 , 'ad_free_months' : 2 , 'video_on_demand' : 2},
{'name': 'acct_3', 'months_subscribed' : 2 , 'ad_free_months' : 1 , 'video_on_demand' : 3}]
combined_total = 0
months_subscribed = 0
ad_free_months = 0
video_on_demand = 0
months_subscribed_total = 0
ad_free_total = 0
video_on_demand_total = 0
inventory(acct_info, months_subscribed, ad_free_months, video_on_demand)
acct_all_total = 0
print('Total for all users is', sum(all_users_collection))
另一种方法可以是使用单个变量并在最后为每个用户总数递增,您将获得所有用户总数
您可以考虑稍微修改您的代码。 我会从你的 function 中移出for loop
,让它只做一件事:为给定帐户做库存。 以下代码只是一个示例,但它为您的问题提供了另一种解决方案。
def inventory(info, months_subscribed, add_free_months, video_on_demand):
acct_all_total = 0 # here you init the total value to 0
if info.get('months_subscribed') == 3:
months_subscribed_total = info.get('months_subscribed') * 18
elif info.get('months_subscribed') < 3:
months_subscribed_total = info['months_subscribed'] * 7
elif info.get('months_subscribed') > 3:
months_subscribed_total = info['months_subscribed'] - 3 * 7 + 18
print(f"User {info.get('name')} has months subscribed total of : $ {months_subscribed_total} ")
if info['ad_free_months'] > 0:
ad_free_total = info.get('ad_free_months') * 2
print(f" User {info.get('name')} total ad free is : {ad_free_total} ")
if info['video_on_demand'] > 0:
video_on_demand_total = info.get('video_on_demand') * 27.99
print(f" User {info.get('name')} total video on demand is : {video_on_demand_total} ")
acct_all_total = int(months_subscribed_total + ad_free_total + video_on_demand_total)
# acct_all_total = [int(acct_all_total)]
print(f"Total for {info.get('name')} is: {acct_all_total} ")
return acct_all_total
请注意,我还评论了# acct_all_total = [int(acct_all_total)]
然后你可以调用它
acct_info = [{'name': 'acct_1', 'months_subscribed': 2, 'ad_free_months': 3, 'video_on_demand': 1},
{'name': 'acct_2', 'months_subscribed': 1, 'ad_free_months': 2, 'video_on_demand': 2},
{'name': 'acct_3', 'months_subscribed': 2, 'ad_free_months': 1, 'video_on_demand': 3}]
combined_total = 0
months_subscribed = 0
ad_free_months = 0
video_on_demand = 0
months_subscribed_total = 0
ad_free_total = 0
video_on_demand_total = 0
acct_all_total = 0
for acct in acct_info:
acct_all_total+=inventory(acct, months_subscribed, ad_free_months, video_on_demand)
print("Tot:",acct_all_total)
Output:
User acct_1 has months subscribed total of : $ 14
User acct_1 total ad free is : 6
User acct_1 total video on demand is : 27.99
Total for acct_1 is: 47
User acct_2 has months subscribed total of : $ 7
User acct_2 total ad free is : 4
User acct_2 total video on demand is : 55.98
Total for acct_2 is: 66
User acct_3 has months subscribed total of : $ 14
User acct_3 total ad free is : 2
User acct_3 total video on demand is : 83.97
Total for acct_3 is: 99
Tot: 212
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.