简体   繁体   English

查找乘法列表索引的总和

[英]Finding The Total Sum of Multiplied List Indexes

I'm newer to Python and I can't seem to figure out the logic for what I am trying to do. 我是Python的新手,似乎无法弄清楚我要做什么的逻辑。 I have 11 lists. 我有11个清单。 10 lists contain the make and model of a car, followed by the value of each car, followed by the quantity of cars of each make/model. 10个列表包含汽车的品牌和型号,其次是每辆汽车的价值,然后是每个品牌/型号的汽车数量。 The 11th list contains the other 10 lists as one main list. 第11个列表包含其他10个列表作​​为一个主列表。

I'm trying to find the product of the value and quantity of each car within each of the 10 lists, then sum the 10 products to get a grand total of the total value of all the cars, and then print that value to the screen. 我正在尝试在10个列表中的每一个中查找每辆汽车的价值和数量的乘积,然后对这10种产品求和以得出所有汽车总价值的总计,然后将该价值打印到屏幕上。

So far I have been able to find the index values that I need, multiply them together, and then print them individually to the screen. 到目前为止,我已经能够找到所需的索引值,将它们相乘,然后将它们分别打印到屏幕上。 I added a counter variable that I was attempting to increment with the values of each calculated product until the end of the loop, at which point the function would stop and the final summed value would print, however this has only resulted with the final product printing to the screen or another number entirely. 我添加了一个计数器变量,该变量试图随着每个计算出的乘积的值递增,直到循环结束,这时函数将停止并打印最终的求和值,但这仅是最终乘积的打印结果完全显示在屏幕上或其他号码上。

I'll add examples for context. 我将为上下文添加示例。

My current code: 我当前的代码:

def totalValue(all):
    print("Total Value")
    x = 0
    for x in range(0, len(all)):
        i = (all[x][1])
        q = (all[x][2])
        iq = i * q
        # qi = iq + iq
        t = '${:,.2f}'.format(iq)
        print(t)
    return all


list0 = []
list1 = []
list2 = []

car1 = ["Chevy Bolt EV", 37495, 2]
car2 = ["Kia Niro", 24485, 23]
car3 = ["VW e-Golf", 32790, 12]
car4 = ["Hyundai Kona", 37495, 3]
car5 = ["Honda Insight", 23725, 4]
car6 = ["Chevrolet Volt", 34395, 14]
car7 = ["Hyundai Ioniq", 23285, 5]
car8 = ["Tesla Model 3", 45200, 1]
car9 = ["Audi e-tron", 75795, 2]
car10 = ["Toyota Prius", 24405, 12]

all = (car1, car2, car3, car4, car5, car6, car7, car8, car9, car10)

print()
totalValue(all)

What I would like to happen: 我想发生的事情:

Total Value
$2,326,615.00

What I have now: 我现在所拥有的:

Total Value
$74,990.00
$563,155.00
$393,480.00
$112,485.00
$94,900.00
$481,530.00
$116,425.00
$45,200.00
$151,590.00
$292,860.00

I had this at one point thinking it should work: 我曾经认为它应该起作用:

def totalValue(all):
    print("Total Value")
    x = 0
    for x in range(0, len(all)):
        i = (all[x][1])
        q = (all[x][2])
        iq = i * q
        qi = iq + iq
    t = '${:,.2f}'.format(qi)
    print(t)
    return all

But it resulted in this: 但这导致了:

Total Value
$585,720.00

I hope what I would like makes sense. 我希望我想要的是合理的。 I will clarify anything if need be. 如果需要的话,我会澄清。 I have a feeling I am missing something rather obvious, but I can't tell what it is. 我有一种感觉,我很想念一些明显的东西,但是我无法分辨它是什么。

Thanks in advance. 提前致谢。

I think you make a mistake here: qi = iq + iq , you can try this: 我认为您在这里犯了一个错误: qi = iq + iq ,您可以尝试以下操作:

def totalValue(all):
    print("Total Value")
    x = 0
    qi = 0
    for x in range(0, len(all)):
        i = (all[x][1])
        q = (all[x][2])
        iq = i * q
        qi += iq
    t = '${:,.2f}'.format(qi)
    print(t)
    return all

and the answer will be $2,326,615.00 答案将是$2,326,615.00


or use sum here: 或在此处使用sum

qi = sum(v[1] * v[2] for v in all)
print('${:,.2f}'.format(qi))

Iterate through the items, each time adding the (value X quantity) of each car to the sum. 遍历项目,每次将每辆汽车的(值X数量)加到总和上。 Then, once the loop has finished and the sum has been calculated, print it. 然后,一旦循环结束并计算出总和,就将其打印出来。 The code: 编码:

def totalValue(all):
    print("Total Value")
    qi = 0
    for x in range(0, len(all)):
        qi += (all[x][1] * all[x][2])
    print('${:,.2f}'.format(qi))

list0 = []
list1 = []
list2 = []

car1 = ["Chevy Bolt EV", 37495, 2]
car2 = ["Kia Niro", 24485, 23]
car3 = ["VW e-Golf", 32790, 12]
car4 = ["Hyundai Kona", 37495, 3]
car5 = ["Honda Insight", 23725, 4]
car6 = ["Chevrolet Volt", 34395, 14]
car7 = ["Hyundai Ioniq", 23285, 5]
car8 = ["Tesla Model 3", 45200, 1]
car9 = ["Audi e-tron", 75795, 2]
car10 = ["Toyota Prius", 24405, 12]

all = (car1, car2, car3, car4, car5, car6, car7, car8, car9, car10)

totalValue(all)

This works. 这可行。 This code printed this: 此代码打印如下:

Total Value 总价值

$2,326,615.00 $ 2,326,615.00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM