我想绕过 headers 并且只有金额列的总和,我该怎么做?
I want to bypass the headers and have just the sum of the amount column, how do I do that?
我的数据是这样的:
'''
Symbol Amount <br />
BB 1000 <br />
TIS 8574 <br />
LIG 1333 <br />
etc... etc... <br />
etc...
'''
由于第一列是字符串,第二列是整数,我如何让代码跳过第一列而只添加第二列?
这是我的:
def total_shares(port_list):
column_sum = 0
for x in port_list:
column_sum = sum(x[1])
return column_sum
port_list 包含一个元组列表。我把那个列表分成两列,现在我只在数量列中添加所有内容。
我现有的代码给我这个错误 -
column_sum = sum(x[1]) TypeError: 'int' object is not iterable
def total_shares(port_list): 是代码的一部分,但不会显示,我不确定为什么
您必须将该行更改为
column_sum += x[1]
如果您想使用 sum()
函数,您也可以使用单行替代方法:
def total_shares(port_list):
return sum(port[1] for port in port_list)
我的数据是这样的:
'''
Symbol Amount <br />
BB 1000 <br />
TIS 8574 <br />
LIG 1333 <br />
etc... etc... <br />
etc...
'''
由于第一列是字符串,第二列是整数,我如何让代码跳过第一列而只添加第二列?
这是我的:
def total_shares(port_list):
column_sum = 0
for x in port_list:
column_sum = sum(x[1])
return column_sum
port_list 包含一个元组列表。我把那个列表分成两列,现在我只在数量列中添加所有内容。
我现有的代码给我这个错误 -
column_sum = sum(x[1]) TypeError: 'int' object is not iterable
def total_shares(port_list): 是代码的一部分,但不会显示,我不确定为什么
您必须将该行更改为
column_sum += x[1]
如果您想使用 sum()
函数,您也可以使用单行替代方法:
def total_shares(port_list):
return sum(port[1] for port in port_list)