Python 实现两个列表里元素对应相乘的方法

yipeiwu_com6年前Python基础

方法一:

结合zip函数,使用map函数:

List1 = [1,2,3,4]
List2 = [5,6,7,8]
List3 = map(lambda (a,b):a*b,zip(List1,List2))
print List3

方法二:

把列表转化为数组,使用np.multiply函数

List = [1,2,3]
List2 = [5,6,7]
List3 = np.multiply(np.array(List1),np.array(List2))
print List3.tolist()

以上这篇Python 实现两个列表里元素对应相乘的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

使用Python中的reduce()函数求积的实例

使用Python中的reduce()函数求积的实例

编写一个prod()函数,可以接受一个list并利用reduce()求积。 from functools import reduce def prod(x,y): return x...

寻找网站后台地址的python脚本

#!/usr/bin/python # This was written for educational purpose only. Use it at your own risk...

Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str

在python的Beautiful Soup 4 扩展库的使用过程中出现了 TypeError: list indices must be integers or slices, no...

Python opencv实现人眼/人脸识别以及实时打码处理

Python opencv实现人眼/人脸识别以及实时打码处理

利用Python+opencv实现从摄像头捕获图像,识别其中的人眼/人脸,并打上马赛克。 系统环境:Windows 7 + Python 3.6.3 + opencv 3.4.2 一、系...

Python通过调用有道翻译api实现翻译功能示例

本文实例讲述了Python通过调用有道翻译api实现翻译功能。分享给大家供大家参考,具体如下: 通过调用有道翻译的api,实现中译英、其他语言译中文 Python代码: # codi...