Element-wise addition of 2 lists?
Use map with operator.add: or zip with a list comprehension: Timing comparisons:
Use map with operator.add: or zip with a list comprehension: Timing comparisons:
Try a list comprehension: This goes through l, multiplying each element by two. Of course, there’s more than one way to do it. If you’re into lambda functions and map, you can even do to apply the function lambda x: x * 2 to each element in l. This is equivalent to: Note that map() returns a map object, not a list, so if you … Read more
For elementwise multiplication of matrix objects, you can use numpy.multiply: Result However, you should really use array instead of matrix. matrix objects have all sorts of horrible incompatibilities with regular ndarrays. With ndarrays, you can just use * for elementwise multiplication: If you’re on Python 3.5+, you don’t even lose the ability to perform matrix multiplication with an operator, because @ does matrix multiplication now: