簡體   English   中英

dict的多個列表中的值總和

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM