Home > Mobile >  How to multiply each digit of an array in the same index in Python?
How to multiply each digit of an array in the same index in Python?

Time:05-04

I have to make this program that multiplies an like this: The first number of the first list of the first array with the first number of the first list of the second array. For example:

Input
array1 = [[1,2,3], [3,2,1]]
array2 = [[4,2,5], [5,6,7]]

So my output must be:

result = [[4,4,15],[15,12,7]]

So far my code is the following:

def multiplyArrays(array1,array2):
    if verifySameSize(array1,array2):
        for i in array1:
            for j in i:
                digitA1 = j
            for x in array2:
                for a in x:
                    digitA2 = a
            mult = digitA1 * digitA2
        return mult
    return 'Arrays must be the same size'

It's safe to say it's not working since the result I'm getting for the example I gave is 7 , not even an array, so, what am I doing wrong?

CodePudding user response:

if you want a simple solution, use numpy:

import numpy as np

array1 = np.array([[1,2,3], [3,2,1]])
array2 = np.array([[4,2,5], [5,6,7]])

result = array1 * array2

if you want a general solution for your own understanding, then it becomes a bit harder: how in-depth do you want the implementation to be? there are many checks for example the same sizes, same types, number of dimensions, etc.

the problem in your code is using for each loop instead of indexing. for i in array1 runs twice, returning a list (first [1,2,3] then [3,2,1]). then you do a for each loop in each list returning a number, meaning you only get 1 number as the output which is the result of the last operation (1 * 7 = 7). You should create an empty list and append your results in a normal for loop (not for each).

so your function becomes:

def multiplyArrays(array1,array2):
    result = []
    for i in range(len(array1)):
        result.append([])
        for j in range(len(array1[i])):
            result[i].append(array1[i][j]*array2[i][j])
    return result

this is a bad idea though because it only works with 2D arrays and there are no checks. Avoid writing your own functions unless you absolutely need to.

CodePudding user response:

You can use zip() to iterate over the lists at the same time:

array1 = [[1,2,3], [3,2,1]]
array2 = [[4,2,5], [5,6,7]]

def multiplyArrays(array1,array2):
    result = []
    for inner1,inner2 in zip(array1,array2):
        inner = []
        for item1,item2 in zip(inner1,inner2):
            inner.append(item1*item2)
        result.append(inner)
    return result

print(multiplyArrays(array1,array2))

Output as requested.

CodePudding user response:

Here are three pure-Python one-liners that yield your expected output, two of which are simply list comprehension versions of the other two answers. List comprehension equivalents are generally more efficient, but you should choose what is most readable for you.

Method 1 @quamrana's, as a list comprehension.

res = [[a * b for a, b in zip(c, d)] for c, d in zip(arr1, arr2)]

Method 2 @OM222O's, as a list comprehension.

res = [[ arr1[i][j] * arr2[i][j] for j in range(len(arr1[0])) ] for i in range(len(arr1))]

Method 3 Similar to Method 1 but makes use of operator.mul(a, b) (returns a * b) from the operator module and the built-in map(function, iterable, ...) function. The map function "[r]eturn[s] an iterator that applies function to every item of iterable, yielding the results." So given two lists a (from array1) and b (from array2), map(operator.mul, a, b) returns an iterator that yields the results of multiplying each element in a with the element in b with the same index. list() converts the results into a list.

res = [list(map(operator.mul, a, b)) for a, b in zip(arr1, arr2)]

Simple Benchmark


Input

from random import randint

arr1 = [[randint(1, 25) for i in range(1_000)] for j in range(1_000)]
arr2 = [[randint(1, 25) for i in range(1_000)] for j in range(1_000)]

Ordered from fastest to slowest

# Method 3
29.2 ms ± 59.1 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
# Method 1
44.4 ms ± 197 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
# Method 2
79.3 ms ± 151 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
# numpy multiplication (inclusive of time required to convert list to array)
81.7 ms ± 122 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

We can see that Method 3 (the operator.mul approach) appears fastest and the numpy approach appears the slowest. There is a big caveat, of course, as the numpy timings included the time required to convert the lists to arrays. In order to make meaningful comparisons, we need to specify whether the input and/or output is a list and/or an array. Clearly, if the inputs are already lists and the results must also be lists, then we can be happy with standard Python approaches.

However, if arr1 and arr2 are already numpy arrays, element-wise multiplication is incredibly fast:

1.47 ms ± 5.2 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

CodePudding user response:

More simpler approach without using any module.

array1 = [[1, 2, 3], [3, 2, 1]]
array2 = [[4, 2, 5], [5, 6, 7]]

result = []

i = 0 
while i < len(array1):
    sub_array1 = array1[i]
    sub_array2 = array2[i]

    a, b, c = sub_array1
    d, e, f = sub_array2

    inner_list = [a * d, b * e, c * f]
    result.append(inner_list)

    i  = 1

print(result)

Output:

[[4,4,15],[15,12,7]]
  • Related