【问题标题】:Multiplying values within two lists and printing them out python将两个列表中的值相乘并将它们打印出来python
【发布时间】:2017-04-21 06:10:11
【问题描述】:

我正在努力做到这一点,以便我可以将每个成本乘以相应的销售产品数量。例如,1.99 * 10、1.49 * 5 等等。另外,我似乎无法弄清楚如何根据列表中的成本打印出最昂贵或最便宜产品的产品名称。我尝试将 product_cost 中的 i 与 product_sold 的相应 i 相乘,但答案似乎很遥远。有谁知道如何解决这个问题?谢谢

但是,使用下面的代码,

# product lists
product_names = ["prime numbers", "multiplication tables", "mortgage calculator"]
product_costs = [1.99, 1.49, 2.49]
product_sold = [10, 5, 15]

def report_product():
    total = 0
    print("Most expensive product:", max(product_costs))
    print("Least expensive product:", min(product_costs))
    for i in range(len(product_costs)):
        total += i * product_sold[i]
    print("Total value of all products:", total)

selection = ""

while selection != "q":
    selection = input("(s)earch, (l)ist, (a)dd, (r)emove, (u)pdate, r(e)port or (q)uit: ")

    if selection == 'q':
        break
    elif selection == 's':
        search_product()
    elif selection == "l":
        list_products()
    elif selection == "a":
        add_products()
    elif selection == "r":
        remove_products()
    elif selection == "u":
        update_products()
    elif selection == "e":
        report_product()
    else:
        print("Invalid option, try again")

print("Thanks for looking at my programs!")

【问题讨论】:

  • 离题:if selection == 'q': break 是多余的,因为 while 循环的条件是 while selection != 'q' 无论如何都会使循环中断
  • 能否显示 search_product、list_products、remove_products、update_products、report_product 功能的代码..??

标签: python list python-3.x function


【解决方案1】:

虽然不一定是最佳的,但您可以使用zip() 用最少的代码得到答案:

def report_product():

    print('Most expensive product:', max(zip(product_costs, product_names))[1])

    print('Least expensive product:', min(zip(product_costs, product_names))[1])

    total_earned = sum(cost * sold for cost, sold in zip(product_costs, product_sold))

    print('Total earned from all products sold: ${:.2f}'.format(total_earned))

输出

Most expensive product: mortgage calculator
Least expensive product: multiplication tables
Total earned from all products sold: $64.70

【讨论】:

    【解决方案2】:
    >>> prod_m = [a*b for a,b in zip(product_costs, product_sold)]
    >>> prod_m
    [19.9, 7.45, 37.35]
    >>> product_names[product_costs.index(max(product_costs))]
    'mortgage calculator'
    >>> product_names[product_costs.index(min(product_costs))]
    'multiplication tables'
    

    您还可以使用 numpy 将两个列表相乘。

    >>> import numpy
    >>> list(numpy.array(product_costs)*numpy.array(product_sold))
    [19.899999999999999, 7.4500000000000002, 37.350000000000001]
    

    【讨论】:

      【解决方案3】:

      要得到一个新的对应索引相乘的数组,

      product_costs = [1.99, 1.49, 2.49]
      product_sold = [10, 5, 15]
      product_mult = list(map(lambda x,y: x*y, product_costs,product_sold))
      

      要找出最贵的产品名称,

      index = product_costs.index(max(product_costs))
      most_expensive = product_names[index]
      

      【讨论】:

      • 代码 [x*y for x in product_costs for y in product_sold] 产生所有九种可能的产品:[19.9, 9.95, 29.85, 14.9, 7.45, 22.35, 24.900000000000002, 12.450000000000001, 37.35] 不是 OP 正在寻找的三个。
      • 代码index = product_mult.index(max(product_mult)) 查找最多的产品,不一定是最最贵的产品。
      • 哦,哎呀,误读了这个问题!再次感谢您的收获,在回答问题时必须更加清晰:/
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 2018-03-22
      相关资源
      最近更新 更多