Python之list对应元素求和

  本次分享将讲述如何在Python中对多个list的对应元素求和,前提是每个list的长度一样。比如:a=[1,2,3], b=[2,3,4], c=[3,4,5], 对a,b,c的对应元素求和,输出应为[6,9,12].
  
方法一:
  直接求解,按照对应元素相加的原则,可先定义一个函数。

def list_add(a,b):c = []for i in range(len(a)):c.append(a[i]+b[i])return cif __name__ == '__main__':a = [1,2,3]b = [2,3,4]c = [3,4,5]print(list_add(list_add(a,b),c))

方法二:
  利用numpy模块求解。

import numpy as np
a = np.array([1,2,3])
b = np.array([2,3,4])
c = np.array([3,4,5])
print(a+b+c)

需要注意的是,a+b+c后的类型为numpy.ndarray.
方法三:
  利用numpy模块的sum()函数进行求解。

import numpy as np
a = [1,2,3]
b = [2,3,4]
c = [3,4,5]
print(np.sum([a,b,c], axis = 0))

其中的axis参数表示纵向求和。



本次分享到此结束,欢迎大家交流~~

注意:本人现已开通两个微信公众号: 因为Python(微信号为:python_math)以及轻松学会Python爬虫(微信号为:easy_web_scrape), 欢迎大家关注哦~~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540560.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[转载] Python中str跟int的转换

参考链接: Python中的类型转换 字符串str转换成int: int_value int(str_value) int转换成字符串str: str_value str(int_value) a100 b666 #int转str类型 print(int转str类型) print(int转str: str(a)) #str转int类型 print(str转int类型…

ot协议是什么_OT的完整形式是什么?

ot协议是什么OT:主题外 (OT: Off Topic) OT is an abbreviation of "Off Topic". OT是“ Off Topic”的缩写 。 It is an expression, which is commonly used in Gmail or messaging platform. It shows that the email that has been sent is irrelev…

[转载] python中字符串编码形式及其所占字节

参考链接: Python中的字节对象与字符串 1.常见字符串编码错误 在使用Python读文件时经常遇到编码问题引起的错误,比如: UnicodeDecodeError: gbk codec cant decode byte 0x80 in position 30: illegal multibyte sequence 遇到这种异…

[AtCoder-ARC073F]Many Moves

题目大意:   有一排n个格子和2枚硬币。   现在有q次任务,每一次要你把其中一枚硬币移到x的位置上,移动1格的代价是1。   两枚硬币不能同时移动,任务必须按次序完成。   现在告诉你两枚硬币初始状态所在的位置a和b&#xf…

ScalavsKotlin

Is Scala better that Kotlin? No..., Is Kotlin better than Scala? No... Scala比Kotlin更好吗? 不...,Kotlin胜过Scala吗? 没有... Both programming languages have their own profits and are for a specific set of development. It…

工业智能相机与基于PC的机器视觉的区别比较

随着科技的日渐成熟,机器视觉得到了飞速发展。由于嵌入式技术的发展,近几年智能相机性能显著提高,越来越多必须依赖于PC处理的应用开始向智能相机平台倾斜。低成本、高可靠性及易于安装维护等优势,使得机器视觉在制造业上的规模性应用越来越普…

[转载] python skimage在图像处理中的用法

参考链接: 在Python中打印单变量和多变量 基于python脚本语言开发的数字图片处理包,比如PIL,Pillow, opencv, scikit-image等。 PIL和Pillow只提供最基础的数字图像处理,功能有限;opencv实际上是一个c库,只是提供了py…

scala元组 数组_Scala中的数组

scala元组 数组Scala中的数组 (Arrays in Scala) An array is a linear data structure with a fixed number of elements. It is a collection that stores a fixed number Arrays in Scalf elements of the same datatype. In Scala, an array is 0 indexed, i.e. the first …

OpenStack —— DevStack一键自动化安装

一、DevStack介绍Devstack目前是支持Ubuntu16.04和CentOS 7,而且Devstack官方建议使用Ubuntu16.04,所以我们使用Ubuntu 16.04进行安装。默认无论是Devstack和OpenStack,都是采用Master的代码进行安装,这样经常会出现,今…

[转载] Python学习笔记——运维和Shell

参考链接: 在C / C,Python,PHP和Java中交换两个变量 目录 什么是运维 运维第一工具-shell编程 shell历史 执行脚本 基本语法 Shell脚本语法 条件测试:test [ if/then/elif/else/fi case/esac for/do/done …

scala java混合_Scala特性混合

scala java混合Scala | 特性混合 (Scala | Trait Mixins ) In Scala, the number of traits can be extended using a class or an abstract class. This is known as Trait Mixins. For extending, only traits, the blend of traits, class or abstract class are valid. If …

Scala铸造

Scala中的类型 (Types in Scala) Type also know as data type tells the compiler about the type of data that is used by the programmer. For example, if we initialize a value or variable as an integer the compiler will free up 4 bytes of memory space and it wi…

/ 卡路里_最大卡路里

/ 卡路里Problem statement: 问题陈述: Shivang is very foodie but he has a diet plan. He has an array of elements indicating the calorie of food he can consume on that day. In his diet plan, he can’t eat on for three consecutive days. But since …

[转载] Python类中的私有变量和公有变量

参考链接: Python中的私有变量 我们这里就直奔主题,不做基础铺垫,默认你有一些Python类的基础,大家在看这篇博客的时候,如果基础知识忘了,可以去菜鸟教程 从一个简单的类开始 class A(): #定义一…

OpenCV探索之路(二十五):制作简易的图像标注小工具

搞图像深度学习的童鞋一定碰过图像数据标注的东西,当我们训练网络时需要训练集数据,但在网上又没有找到自己想要的数据集,这时候就考虑自己制作自己的数据集了,这时就需要对图像进行标注。图像标注是件很枯燥又很费人力物力的一件…

固件的完整形式是什么?

FW:前进 (FW: Forward) FW is an abbreviation of "Forward". FW是“ Forward”的缩写 。 It is an expression, which is commonly used in Gmail or messaging platform. It is also written as FWD or Fwd or Fw. It shows that the email has been s…

[转载] python __slots__ 详解(上篇)

参考链接: Python的__name __(特殊变量) python中的new-style class要求继承Python中的一个内建类型, 一般继承object,也可以继承list或者dict等其他的内建类型。 在python新式类中,可以定义一个变量__slots__,它的作…

委托BegionInvoke和窗体BegionInvoke

委托BegionInvoke是指通过委托方法执行多线程任务,例如: //定义委托成员变量 delegate void dg_DeleAirport(); //指定委托函数 dg_DeleAirport dga AirportBLL.DeleteHistoryTransAirport; //通过BeginInvoke以异步线程方式执行委托函数,可…

图论 弦_混乱的弦

图论 弦Problem statement: 问题陈述: You are provided an input string S and the string "includehelp". You need to figure out all possible subsequences "includehelp" in the string S? Find out the number of ways in which the s…

[转载] Python列表操作

参考链接: Python中的基本运算符 Python列表: 序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推; Python有6个序列的…