Python SUMPRODUCT of elements in nested list

2024/9/21 4:29:33

I have two nested lists:

a = [[1,2,3],[2,4,2]]
b = [[5,5,5],[1,1,1]]

I want to multiply and SUMPRODUCT each group of elements to get

c = [[30],[8]]

Which result from = [[1*5+2*5+3*5],[2*1,4*1,2*1]]

I´ve tried doing:

c = sum(x * y for x, y in zip(a, b))

But I get "can't multiply sequence by non-int of type 'list'"

Is there a simple list comprehension way to do this avoiding for loops?

Answer

You can implement the dotproduct itertools recipes:

import operatordef dotproduct(vec1, vec2):return sum(map(operator.mul, vec1, vec2))

Code

a = [[1,2,3], [2,4,2]] 
b = [[5,5,5], [1,1,1]][[dotproduct(x, y)] for x, y in zip(a, b)]
# [[30], [8]]
https://en.xdnf.cn/q/72088.html

Related Q&A

Modifying viridis colormap (replacing some colors)

Ive searched around and found things that came close to working but nothing exactly suiting what I need. Basically, I really like the viridis colormap as a starting point. However, I would like to repl…

gtk minimum size

Is there an easy way to request that a GTK widget have a minimum width/height? I know you can do it on the column of a TreeView, but is it available for general widgets?

How do I convert a json file to a python class?

Consider this json file named h.json I want to convert this into a python dataclass. {"acc1":{"email":"[email protected]","password":"acc1","name&…

PyTorch how to compute second order Jacobian?

I have a neural network thats computing a vector quantity u. Id like to compute first and second-order jacobians with respect to the input x, a single element. Would anybody know how to do that in PyTo…

Tensorflow setup on RStudio/ R | CentOS

For the last 5 days, I am trying to make Keras/Tensorflow packages work in R. I am using RStudio for installation and have used conda, miniconda, virtualenv but it crashes each time in the end. Install…

Cant import soundfile

Im using Anaconda and Im trying to import soundfile/pysoundfile. I installed the package by running conda install -c conda-forge pysoundfile and I think it succeeded because when I run conda list it sh…

Most efficient way to multiply a small matrix with a scalar in numpy

I have a program whose main performance bottleneck involves multiplying matrices which have one dimension of size 1 and another large dimension, e.g. 1000: large_dimension = 1000a = np.random.random((1…

MultiValueDictKeyError / request.POST

I think I hav a problem at request.POST[title]MultiValueDictKeyError at /blog/add/post/"title"Request Method: GETRequest URL: http://119.81.247.69:8000/blog/add/post/Django Version: 1.8.…

How can I auto run py.test once a relative command has been change?

Via autonose or nosy, it will automatically run the nosetests once the some tests file or the relative files have been changes. I would like to ask that whether py.test provides the similar function fo…

Publish a post using XML-RPC WordPress API and Python with category

Im doing a migration from a website to another one which use Wordpress. I created new custom types for my needs (with the plugin Custom Post Types), and I created categories for each custom type.I then…