python:逐点列表总和
发布时间:2020-12-20 13:04:28 所属栏目:Python 来源:网络整理
导读:Input: two lists (list1,list2) of equal lengthOutput: one list (result) of the same length,such that: result[i] = list1[i] + list2[i] 有没有简洁的方法呢?或者这是最好的: # Python 3assert len(list1) == len(list2)result = [list1[i] + list2[
Input: two lists (list1,list2) of equal length Output: one list (result) of the same length,such that: result[i] = list1[i] + list2[i] 有没有简洁的方法呢?或者这是最好的: # Python 3 assert len(list1) == len(list2) result = [list1[i] + list2[i] for i in range(len(list1))] 解决方法
您可以使用builtin
zip函数,也可以使用add it映射两个列表.像这样:
from operator import add map(add,list1,list2) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |