简体   繁体   English

dict的多个列表中的值总和

[英]Sum of values in a multiple lists of a dict

I am new to python and have been trying to add values that I get from iterating over a list of dictionaries.我是 python 的新手,并且一直在尝试添加通过迭代字典列表获得的值。

I keep running into 'builtin_function_or_method' object is not iterable' error message or unsupported type.我一直遇到'builtin_function_or_method' object is not iterable' 错误消息或不受支持的类型。 Any help would be much appreciated.任何帮助将非常感激。

here is my code:这是我的代码:

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 so far is: 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] 

What i am trying to sum up is the total for all of the users.我要总结的是所有用户的总数。 I manage to get a total for each user, but i then want add the totals of that.我设法得到每个用户的总数,但我想添加总数。 Thank you.谢谢你。

I modified your inventory function, now it contains a list before the loop start and it holds all the totals of the users that you are printing at the bottom of the loop, at the end using the sum function the grand total of all users can be calculated我修改了您的库存 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))

The other approach can be to use a single variable and increment for every total of a user at the end you will get total of all users另一种方法可以是使用单个变量并在最后为每个用户总数递增,您将获得所有用户总数

you can take into consideration modifying your code a little bit your code.您可以考虑稍微修改您的代码。 I would move out the for loop from your function to make it do just one thing: do the inventory for a given account.我会从你的 function 中移出for loop ,让它只做一件事:为给定帐户做库存。 The following code is just an example, but it provides an alternative solution for your question.以下代码只是一个示例,但它为您的问题提供了另一种解决方案。

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

Please note I also commented the # acct_all_total = [int(acct_all_total)]请注意,我还评论了# acct_all_total = [int(acct_all_total)]

Then you can call it然后你可以调用它

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: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM