Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数对字典中的总值求和_Python_Shopping Cart - Fatal编程技术网

Python 函数对字典中的总值求和

Python 函数对字典中的总值求和,python,shopping-cart,Python,Shopping Cart,我有一个dict,其中有几个值项 inventory = {847502: ['APPLES 1LB', 2, 50], 847283: ['OLIVE OIL', 1, 100], 839529: ['TOMATOS 1LB', 4, 25], 483946: ['MILK 1/2G', 2, 50], 493402: ['FLOUR 5LB', 2, 50], 485034: ['BELL PEPPERS 1LB', 3, 50]} 我想创建一个函

我有一个dict,其中有几个值项

   inventory = {847502: ['APPLES 1LB', 2, 50], 847283: ['OLIVE OIL', 1, 100], 839529: ['TOMATOS 1LB', 4, 25], 
                 483946: ['MILK 1/2G', 2, 50], 493402: ['FLOUR 5LB', 2, 50], 485034: ['BELL PEPPERS 1LB', 3, 50]}
我想创建一个函数来获取值项的总和,即sum((2*50)+(1*100)等) 我想我就快到了,但这似乎只是增加了第一个价值

def total_and_number(dict):
    for values in dict.values():
        #print(values[1]*values[2])
        total =0
        total += (values[1]* values[2])
        return(total)


total_and_number(inventory)
使用:

使用:


您应该为循环外代码定义变量total

result = sum([ value[1]*value[2] for value in inventory.values()]


您应该为循环外代码定义变量total

result = sum([ value[1]*value[2] for value in inventory.values()]


返回行和总计行放错了位置。这返回650

inventory = {847502: ['APPLES 1LB', 2, 50],
             847283: ['OLIVE OIL', 1, 100], 839529: ['TOMATOS 1LB', 4, 25], 
             483946: ['MILK 1/2G', 2, 50], 493402: ['FLOUR 5LB', 2, 50],
             485034: ['BELL PEPPERS 1LB', 3, 50]
}

def total_and_number(dict):
    total = 0
    for values in dict.values():
        total += values[1]*values[2]
    return(total)

total_and_number(inventory)

返回行和总计行放错了位置。这返回650

inventory = {847502: ['APPLES 1LB', 2, 50],
             847283: ['OLIVE OIL', 1, 100], 839529: ['TOMATOS 1LB', 4, 25], 
             483946: ['MILK 1/2G', 2, 50], 493402: ['FLOUR 5LB', 2, 50],
             485034: ['BELL PEPPERS 1LB', 3, 50]
}

def total_and_number(dict):
    total = 0
    for values in dict.values():
        total += values[1]*values[2]
    return(total)

total_and_number(inventory)

您应该尝试通过项目的键值访问这些项目:

def total_and_number(dictis):
    total = 0
    for key in list(dictis.keys()):
        print(key)
        total += (dictis[key][1]*dictis[key][2])
    return(total)

它确实会返回所需的预期值。

您应该尝试通过项目的键值访问项目:

def total_and_number(dictis):
    total = 0
    for key in list(dictis.keys()):
        print(key)
        total += (dictis[key][1]*dictis[key][2])
    return(total)
它确实会返回所需的预期值。

请尝试以下操作:

x = {
    847502: ['APPLES 1LB', 2, 50], 847283: ['OLIVE OIL', 1, 100], 839529: ['TOMATOS 1LB', 4, 25], 
    483946: ['MILK 1/2G', 2, 50], 493402: ['FLOUR 5LB', 2, 50], 485034: ['BELL PEPPERS 1LB', 3, 50]
}

print(sum([x[i][1]*x[i][2] for i in x.keys()]))
输出:

C:\Users\Desktop>py x.py
650
编辑:对于您自己的代码,您需要从循环中取出
total=0
return total

def total_and_number(dict):
    total = 0
    for values in dict.values():
        total += (values[1]*values[2])
    return(total)


print(total_and_number(x))
输出:

C:\Users\Desktop>py x.py
650
试试这个:

x = {
    847502: ['APPLES 1LB', 2, 50], 847283: ['OLIVE OIL', 1, 100], 839529: ['TOMATOS 1LB', 4, 25], 
    483946: ['MILK 1/2G', 2, 50], 493402: ['FLOUR 5LB', 2, 50], 485034: ['BELL PEPPERS 1LB', 3, 50]
}

print(sum([x[i][1]*x[i][2] for i in x.keys()]))
输出:

C:\Users\Desktop>py x.py
650
编辑:对于您自己的代码,您需要从循环中取出
total=0
return total

def total_and_number(dict):
    total = 0
    for values in dict.values():
        total += (values[1]*values[2])
    return(total)


print(total_and_number(x))
输出:

C:\Users\Desktop>py x.py
650

看起来每个值都是一个列表(尽管它可能是一个元组):

因此,迭代并直接求和应该很容易:

sum(qty*eachprice for _, qty, eachprice in inventory.values())

看起来每个值都是一个列表(尽管它可能是一个元组):

因此,迭代并直接求和应该很容易:

sum(qty*eachprice for _, qty, eachprice in inventory.values())

return
应该从
for
循环中退出。
return
应该从
for
循环中退出。这并没有改善任何事情,问题是return语句发生在循环的第一次迭代中,每次迭代的总计都被重置为0。这并没有改善任何事情,问题是返回语句出现在循环的第一次迭代中,并且每次迭代都将total重置为0。使用
x.values()
更容易,没有?这是肯定的,但问题的作者已经使用了它,我只是想从另一个角度来探讨,以帮助其他不知道这一点的人。使用
x.values()
更容易,没有?这是肯定的,但问题的作者已经使用了它,我只是想从另一个角度来探讨,以便使其他不知道这一点的人受益。