如何执行两个列表的按元素相乘? [英] How to perform element-wise multiplication of two lists?

查看:380
本文介绍了如何执行两个列表的按元素相乘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行元素明智的乘法,以将两个列表按值在Python中相乘,就像我们在Matlab中可以做到的那样.

I want to perform an element wise multiplication, to multiply two lists together by value in Python, like we can do it in Matlab.

这就是我在Matlab中要做的事情.

This is how I would do it in Matlab.

a = [1,2,3,4]
b = [2,3,4,5]
a .* b = [2, 6, 12, 20]

对于axx * yby的每个组合,列表理解将给出16个列表条目.不确定如何映射.

A list comprehension would give 16 list entries, for every combination x * y of x from a and y from b. Unsure of how to map this.

如果有人对为什么感兴趣,我有一个数据集,并想将其乘以Numpy.linspace(1.0, 0.5, num=len(dataset)) =).

If anyone is interested why, I have a dataset, and want to multiply it by Numpy.linspace(1.0, 0.5, num=len(dataset)) =).

推荐答案

使用列表理解和zip():.

[a*b for a,b in zip(lista,listb)]

这篇关于如何执行两个列表的按元素相乘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆