【问题标题】:how can i make summary transaction from dictionary?如何从字典中进行摘要交易?
【发布时间】:2020-09-25 09:42:43
【问题描述】:

我有这样的代码:

bought = {'Banana':2, 'Avocado':3, 'Cherries':1}
price_of = {'Apple': 6,
'Avocado': 5,
'Banana': 3,
'Blackberries': 10,
'Blueberries': 12,
'Cherries': 7,
'Pineapple': 7}

bought = sorted(bought) 
def print_summary(items,fprice):
   items = bought
   for item, n in fprice.items():
      print(n, item, ':', n * items[item])
      print('total', sum(n * items[item])

print_summary(bought,price_of)

但我得到这样的输出:

enter image description here

我想让输出是这样的:

3 Avocado : 15
2 Banana : 6
1 Cherries : 7
total : 28

【问题讨论】:

  • counter_itemchart 是什么

标签: python function loops dictionary


【解决方案1】:

这里您想根据键值以相反的顺序对字典进行排序,因此您必须在代码中添加它。这里主要关注的是排序,所以我以简单的方式完成了值的乘法。

bought = {'Banana':2, 'Avocado':3, 'Cherries':1}
price_of = {'Apple': 6,
'Avocado': 5,
'Banana': 3,
'Blackberries': 10,
'Blueberries': 12,
'Cherries': 7,
'Pineapple': 7}

bought['Banana'] *= price_of['Banana']
bought['Avocado'] *= price_of['Avocado']
bought['Cherries'] *= price_of['Cherries']

lst = list()
for k,v in bought.items():
    newtup = (v,k)
    lst.append(newtup)

lst = sorted(lst, reverse = True)
n=3
total = 0
for k,v in lst:
    print(n,' ',v,' : ',k)
    total = total + k
    n = n-1
print('total :', total) 

这段代码的输出是:

3   Avocado  :  15
2   Cherries  :  7
1   Banana  :  6
total : 28

【讨论】:

    【解决方案2】:

    我弄得有点乱,但可以工作

    bought = {'Banana':2, 'Avocado':3, 'Cherries':1}
    price_of = {'Apple': 6,
    'Avocado': 5,
    'Banana': 3,
    'Blackberries': 10,
    'Blueberries': 12,
    'Cherries': 7,
    'Pineapple': 7}
    
    #Creating a new sorted dictionary named b
    a = sorted(bought)
    b = {}
    for fruit in a:
        b[fruit] = bought.get(fruit)
    #Making bought be b
    bought = b
    
    def summary(items, price):
        total = 0
        for i in items:
            print(f" {items.get(i)} {i} : {items.get(i)*price.get(i)}")
            total = total + items.get(i)*price.get(i)
        print(f" Total {total}")
    
    summary(bought, price_of)
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:
      bought = {'Banana':2, 'Avocado':3, 'Cherries':1}
      price_of = {'Apple': 6,
      'Avocado': 5,
      'Banana': 3,
      'Blackberries': 10,
      'Blueberries': 12,
      'Cherries': 7,
      'Pineapple': 7}
      
      # bought = sorted(bought)
      def print_summary(items,fprice):
      #    items = counter_item(chart)
      #    for item, n in fprice.items():
      #       print(n, item, ':', n * items[item])
      #       print('total', sum(n * items[item]))
          total = 0
          for i, k in enumerate(sorted(items, key= lambda x: items[x], reverse=True )):
              print(items[k], k, ':', items[k] * fprice[k])
              total += items[k] * fprice[k]
          print("total : {}".format(total))
      
      print_summary (bought,price_of)
      
      3 Avocado : 15
      2 Banana : 6
      1 Cherries : 7
      total : 28
      

      【讨论】:

        【解决方案4】:
        for fruit, quantity in sorted(bought.items()):
            print(f"{quantity} {fruit} : {quantity * price_of[fruit]}")
        print(f"total : {sum(quantity * price_of[fruit] for fruit, quantity in bought.items())}")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-26
          • 2012-05-08
          • 1970-01-01
          • 1970-01-01
          • 2022-10-24
          • 1970-01-01
          相关资源
          最近更新 更多