Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

python 3.x - Sum of x numbers of list of integer

I have many lists of integer and want to calculate the sum numbers for each list My lists sample:

list_1 = [2,4,5,6,8,9,0]
list_2 = [43,435,324,65,23]
list_n = [...]
...

This is my code:

res = []
for i in range(len(list_n):
    x.append(list_n[i])
res = sum(x)
print(res)

I am able to get sum of each list but when I tried to export the file with this code:

d = [a, b, c, res]
Dat_exp = zip_longest(*d, fillvalue = '')
with open(CurrTime.strftime('%d %m %H %M') + "_RouteData.csv","w+", newline='') as self.Dat_route:
wr = csv.writer(self.Dat_route) 
wr.writerow(('name_of_a','name_of_b','name_of_c','name_of res'))
wr.writerows(Dat_exp)

everything worked fine with [a], [b] and [c] the output file was awesome as I expected until I put my 'res' inside the output code, the error appeared as below:

...., in Dat_exp = zip_longest(*d, fillvalue = '') TypeError: 'int' object is not iterable

Other 3 variables are in array forms and didn't have to do sum at all. It's strange when I hovered my mouse to the res variable it appears to have 2 types in 2 lines like this:

res: list

res: int

So I think that the res in my main script is type list but the one in variable d of output code is in 'int' type. I just want the output as same as the others. The code did count all total values for each list when running as showed in terminal.

Edit: I tried adding [res] in as d = [a, b, c, [res]], this time the output appeared but only the total number of the last list appear in output file.

Any help would be appreciated!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...