How to sum the elements of a python list

Release: 2019-12-31 16:55:28
Original
34101 people have browsed it

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)
Copy after login

Method two:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!