Home  >  Article  >  Backend Development  >  How to sum the elements of a python list

How to sum the elements of a python list

尚
Original
2019-07-26 15:29:1034114browse

How to sum the elements of a python list

Recommended manual:Python basic introductory tutorial

Python implements the sum of corresponding elements in a list of two methods.

Method one: Use zip() method:

# -*- coding: utf-8 -*-
import math
import numpy as np
a= [1,2,3] 
b =[4,5,6] 
 
#方法1
c=[]
for i,j in zip(a,b):
    summ=i+j
    c.append(summ)
print(c)

Method two:

#方法2
d=[]
for i in range(0,len(a)):
summm=a[i]+b[i]
d.append(summm)
print(d)

Recommended: Python graphic tutorial

Recommended related articles:
1.How to add up the numbers in the list in Python
2.How to count the elements in the Python list Frequency of occurrence? (Code example)
3.How to merge elements of sublists in Python?
Related video recommendations:
1.Little Turtle Zero Basics Learning Python Video Tutorial

The above is the detailed content of How to sum the elements of a python list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn