2021年1月22日星期五

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

Any help would be appreciated!

https://stackoverflow.com/questions/65854994/sum-of-x-numbers-of-list-of-integer January 23, 2021 at 09:55AM

没有评论:

发表评论