简体   繁体   中英

I want to bypass the headers and have just the sum of the amount column, how do I do that?

This is what my data looks like:

'''
Symbol         Amount <br />
BB                1000 <br />
TIS            8574 <br />
LIG            1333 <br />
etc...          etc... <br />
etc...
'''

Since the first column is strings and the second is integers, how do I get the code to skip over the first column and only add the second?

This is what I have:

def total_shares(port_list):  
    column_sum = 0
    for x in port_list:
        column_sum = sum(x[1]) 
    return column_sum

port_list contains a list of tuples. I took that list and made it into two columns, now I was to add everything in the amount column only.

my existing code gives me this error -

column_sum = sum(x[1]) TypeError: 'int' object is not iterable

the def total_shares(port_list): is apart of the code but it won't show as it, I'm not sure why

You must change that line to

column_sum += x[1]

You can also use a one-liner alternative if you want to use the sum() function:

def total_shares(port_list):
    return sum(port[1] for port in port_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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