如何将两个列表作为输入并从其他两个列表中进行算术运算得到输出?

时间:2018-12-21 14:00:07

标签: python list

这是一个作业问题。我只是在问解决这个问题的方法,而不是整个解决方案。

问题如下:

  

编写一个Python程序来计算将要支付的帐单金额   客户根据所购买的宝石和数量清单。任何   购买总账单金额超过30000卢比的订单,有权获得5%   折扣。如果客户要求的任何宝石都无法在   商店,然后考虑总账单金额为-1。

     

假设客户要求的任何宝石数量总是   大于0。无论在何处执行区分大小写的比较   适用。

我已经尝试过考虑下面给出的输入来实现该程序,但是无法理解如何从列表中获取输入以及如何进行特定的算术运算以获取所需的输出。

def calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity):
   bill_amount = 0
   # this is the portion where I should write my logic

   return bill_amount

# List of gems available in the store
gems_list = ["Emerald", "Ivory", "Jasper", "Ruby", "Garnet"]

# Price of gems available in the store. gems_list and price_list have one-to-one correspondence
price_list = [1760, 2119, 1599, 3920, 3999]

# List of gems required by the customer
reqd_gems = ["Ivory", "Emerald", "Garnet"]

# Quantity of gems required by the customer. reqd_gems and reqd_quantity have one-to-one correspondence
reqd_quantity = [3, 10, 12]

bill_amount = calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity)
print(bill_amount)

预期结果是帐单总额。

3 个答案:

答案 0 :(得分:0)

这是解决方案和逻辑:

说明:使用dict(zip())在函数中创建宝石和相应价格的字典。然后遍历所需的宝石,并获得该宝石的相应价格/价格,并将其乘以数量。为了从列表中获取数量,我使用了枚举来访问相应的索引。

请继续将其添加到for循环内的bill_amount中,并在for循环外返回

def calculate_bill_amount(gems_list, price_list, reqd_gems,reqd_quantity):
    bill_amount=0
    d = dict(zip(gems_list, price_list))
    for i, gem in enumerate(reqd_gems):
        bill_amount += d[gem]*reqd_quantity[i]

    return bill_amount

bill_amount=calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity)
print(bill_amount)
# 71945

答案 1 :(得分:0)

类似于@Bazingaa的答案,我建议您为此作业使用映射(Python dict)。

首先,您可以像这样创建“宝石价格”映射:

price_map = {}  # This is an empty dict
for i in range(len(gems_list)):
    price_map[gems_list[i]] = price_list[i]

然后,对于自定义想要的每颗宝石,您可以检查其是否有库存:

wanted = "Invalid"
has_gem = wanted in price_map
#         ^^^^^^^^^^^^^^^^^^^

您将能够处理所需的宝石无效的异常情况。

您可以通过字典查找获取价格:

wanted = "Ruby"
price = price_map[wanted]  # this will fetch 3920

如果您了解列表理解,字典理解,价值分解和内置的zip方法等高级技术,那么它很棒,您将能够编写较短的程序:

price_map = {gem: price for gem, price in zip(gems_list, price_list)}

同样,如果您了解Python的异常处理,则可以使用try ... except ...块来处理所需的gem无效的情况,而无需事先验证它们。

try:
    bill_amount += quantity * price_map[gem]
except KeyError:
    # One of the gems isn't available
    return -1

如果需要直接解决方案,只需阅读Bazingaa's answer

答案 2 :(得分:0)

自然地,这个问题非常适合使用字典,但是由于这是家庭作业,我认为您的教授希望您学习有关遍历列表的更多信息。当然,我可能是错的。无论如何,这是没有字典的解决方案

def calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity):
   bill_amount = 0
   for i in range(len(reqd_gems)):
       ## we walk through every item in reqd_gems
       ## for every item in the list, we assume it's not available until we have found it in gems_list
       itemIsAvailable = False
       for j in range(len(gems_list)):
           # for every item in in reqd_gems, we walk through all the items in gems_list until we find the same item
           if reqd_gems[i] == gems_list[j]:
               itemIsAvailable = True
               # if we have found the same item, then it's available
               bill_amount += price_list[j] * reqd_quantity[i]
               # and we use the fact that the two pairs of lists have 1-to-1 correspondence to calculate the amount to add to bill_amount

       if not itemIsAvailable:
           return -1

   if bill_amount > 30000:
       return bill_amount * 0.95
   return bill_amount