2

In my python code, I have an array that has different size inside like this

arr = [
    [1],
    [2,3], 
    [4],
    [5,6,7],
    [8],
    [9,10,11]
]

I want to multiply them by 10 so it will be like this

arr = [
        [10],
        [20,30], 
        [40],
        [50,60,70],
        [80],
        [90,100,110]
    ]

I have tried arr = np.multiply(arr,10) and arr = np.array(arr)*10

it seems that both are not working for different size of nested array because when I tried using same size nested array, they actually works just fine

2
  • arr is a nested listed for a numpy array? Commented Dec 20, 2022 at 9:29
  • 2
    [[i*10 for i in sublist] for sublist in arr] without numpy Commented Dec 20, 2022 at 9:30

2 Answers 2

1

It is best to just use a nested loop :

arr = [
    [1],
    [2,3], 
    [4],
    [5,6,7],
    [8],
    [9,10,11]
]

def matrix_multiply_all(matrix,nb):
    return list(map(lambda arr : list(map(lambda el : el*nb,arr)),matrix))
print(matrix_multiply_all(arr,10))
Sign up to request clarification or add additional context in comments.

Comments

1

You can do with list comprehension as sahasrara62 mentioned if it is a list:

[[i*10 for i in x] for x in arr]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.