Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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 - Fatal编程技术网

Python 如何计算列表中的多个对象值

Python 如何计算列表中的多个对象值,python,Python,我试图从数据库中添加对象的总和,但到目前为止,我没有发现成功 test = [{'shares': 5}, {'shares': 1}] # print sum of the two shares (5+1=6) 我尝试使用test['shares']打印,但结果是错误: 列表索引必须是整数或片,而不是str 我尝试了sum(test.value()),但产生了错误 “列表”对象没有属性“值” 和sum(test)创建: +:“int”和“dict”的操作数类型不受支持 似乎这些对象被包装在一

我试图从数据库中添加对象的总和,但到目前为止,我没有发现成功

test = [{'shares': 5}, {'shares': 1}]
# print sum of the two shares (5+1=6)
我尝试使用
test['shares']
打印,但结果是错误:

列表索引必须是整数或片,而不是str

我尝试了
sum(test.value())
,但产生了错误

“列表”对象没有属性“值”

sum(test)
创建:

+:“int”和“dict”的操作数类型不受支持

似乎这些对象被包装在一个列表中,但我不知道如何打开它?

一个好方法是

sum(x['shares'] for x in test)
为什么这样做有效

给定一个字典
d={'shares':7}
,然后调用
d['shares']
来获取值
7

您希望对列表中的每个词典执行此操作
test
,因此您希望查看
test
中每个
x
x['shares']

在Python中,
sum
函数可以接受一个叫做“生成器表达式”的东西,这就是我们在这里所做的

sum(x['shares'] for x in test)
为什么这样做有效

给定一个字典
d={'shares':7}
,然后调用
d['shares']
来获取值
7

您希望对列表中的每个词典执行此操作
test
,因此您希望查看
test
中每个
x
x['shares']


在Python中,
sum
函数可以接受一个称为“生成器表达式”的东西,这就是我们在这里所做的。

test
是一个列表。您希望通过索引访问其元素:

print(test[0]['shares'] + test[1]['shares'])
或者,如果该列表中有许多对象:

print(sum( x['shares'] for x in test ))


test
是一个列表。您希望通过索引访问其元素:

print(test[0]['shares'] + test[1]['shares'])
或者,如果该列表中有许多对象:

print(sum( x['shares'] for x in test ))


您正在将数据放入一个
列表中,并将其用作字典,以便更好地对其进行结构化


{'shares':[5,1]}

您正在将数据放入一个
列表
中,并将其用作字典,以便更好地构造数据


{'shares':[5,1]}

你有一个字典列表。您必须从列表中选择字典编号,然后使用键访问如下值:

test[0]['shares']

下次你可以在网上轻松找到它

您有一个字典列表。您必须从列表中选择字典编号,然后使用键访问如下值:

test[0]['shares']

下次你可以在网上轻松找到它

使用下面的代码,即使对象值不同,您也可以对所有对象值进行调整

test = [{'shares': 5}, {'shares': 1}]

total = 0
for each in test:
  for i in each:
    total += each[i]

print(total)

使用下面的代码,即使对象值不同,您也可以对所有对象值进行调整

test = [{'shares': 5}, {'shares': 1}]

total = 0
for each in test:
  for i in each:
    total += each[i]

print(total)

请注意格式化的约定。请看我的编辑。请注意格式的约定。请看我的编辑。做什么:),你也可以像
sum(map['shares'])那样计算股票。
做什么:),你也可以像
sum(map['shares'])那样计算股票。
谢谢!这似乎解决了我所有的问题,特别是考虑到数据库中有如此多的共享需要处理。谢谢谢谢这似乎解决了我所有的问题,特别是考虑到数据库中有如此多的共享需要处理。谢谢