【问题标题】:How to extract keys with its values and add values to print the total in python dictionary?如何提取键及其值并添加值以在 python 字典中打印总数?
【发布时间】:2020-02-16 13:12:05
【问题描述】:

这里有一家餐厅的菜单和价格。我想检查订单并添加价格并打印订单总价。但我无法提取特定订单的键值。如何同时显示订单及其价格

malesuada : $14.00
抱歉,我们没有披萨
maecenas : $12.00

总价:$26.00

menu_items = {
    'nulla aliquam': 15.00,
    'malesuada': 14.00,
    'feugiat ipsum': 9.00,
    'maecenas': 12.00,
    'fermentum mass': 23.00
}
ordered_items = {
    'maecenas',
    'pizza',
    'malesuada'
}
for item in ordered_items:
    if item in menu_items.keys():
      print(item)
    else:
      print("sorry we dont have ",item)

【问题讨论】:

    标签: python python-3.x dictionary key key-value-store


    【解决方案1】:

    两件事:

    • 您无需使用.keys() 来检查字典中是否存在键

    • 您使用字典索引访问价格:dictionary[key] -> value

    total = 0
    for item in ordered_items:
        if item in menu_items:
            print('{} : ${:2f}'.format(item, menu_items[item]))
            total += menu_items[item]
        else:
            print('Sorry we don\'t have {}'.format(item))
    
    print('Total price : ${:2f}'.format(total))
    

    【讨论】:

      【解决方案2】:

      您可以使用列表解析来获取ordered_items 中项目的菜单价格,请注意您为ordered_items 创建的结构是set 而不是字典。

      menu_items = {
          'nulla aliquam': 15.00,
          'malesuada': 14.00,
          'feugiat ipsum': 9.00,
          'maecenas': 12.00,
          'fermentum mass': 23.00
      }
      ordered_items = {
          'maecenas',
          'pizza',
          'malesuada'
      }
      
      totalPrice = sum([v for k,v in menu_items.items() if k in ordered_items])
      print(totalPrice)
      

      输出:

      26.0
      

      列表推导是执行以下 for 循环的更好方法:

      编辑为您指定订单中每个项目的打印。

      total_price = 0 
      for item in ordered_items:
          if item in menu_items:
              print(f"{item} : ${menu_items[item]} ")
              total_price += menu_items[item]
          else:
              print(f"Sorry, we don't have {item}")
      print(f'Total : ${total_price}')
      

      输出:

      maecenas : $12.0

      抱歉,我们没有披萨

      malesuada : $14.0

      总计:$26.0

      要将所有这些都打印在一行上,请将每个语句附加到每个循环的一个字符串并在末尾打印:

      total_price = 0 
      printString = ''
      for item in ordered_items:
          if item in menu_items:
              printString += f"{item} : ${menu_items[item]} "
      #        print(f"{item} : ${menu_items[item]} ")
              total_price += menu_items[item]
          else:
      #        print(f"Sorry, we don't have {item}")
              printString += f"Sorry, we don't have {item} "
      #print(f'Total : ${total_price}')
      printString +=  f"Total : ${total_price} "
      
      print(printString)
      

      输出:

      maecenas : $12.0 抱歉,我们没有披萨 malesuada : $14.0 总计 : $26.0

      【讨论】:

      • 你的回答对我很有帮助。谢谢,但我怎样才能在同一行显示订购的商品及其价格?像这样:malesuada:$14.00 抱歉,我们没有披萨 maecenas:$12.00
      • @Deep_gtm77 当然,我已经编辑了答案以包含它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多