noazet abdo
noazet abdo

Reputation: 113

getting the inverse of each element and putting it in array in python

guys i am totally new to programming so i want your help. i want to make a function that takes a 2 dimensional array and maps each non zero element to it's inverse and putting it in new_arr but it doesn't give me what I need here is my code:

def question(arr):    
    for i in range(3):
        for j in range(3):
            z = arr[i][j]
            new_arr[i][j] = z^-1
    return new_arr[i][j]

question([[70,52,13,67],[90,48,57,26],[43,45,67,89],[88,65,44,23]])

so any help please ?

Upvotes: 1

Views: 2324

Answers (3)

wwii
wwii

Reputation: 23753

If you can use numpy and a value of inf is acceptable for any items that are 0 then:

import numpy as np
a = np.array([[70,52,13,67],[90,48,57,26],[43,45,67,89],[88,65,44,0]],
              dtype = np.float32)

>>> 
>>> 1/a
array([[ 0.01428571,  0.01923077,  0.07692308,  0.01492537],
       [ 0.01111111,  0.02083333,  0.01754386,  0.03846154],
       [ 0.02325581,  0.02222222,  0.01492537,  0.01123596],
       [ 0.01136364,  0.01538462,  0.02272727,         inf]], dtype=float32)
>>> a**-1
array([[ 0.01428571,  0.01923077,  0.07692308,  0.01492537],
       [ 0.01111111,  0.02083333,  0.01754386,  0.03846154],
       [ 0.02325581,  0.02222222,  0.01492537,  0.01123596],
       [ 0.01136364,  0.01538462,  0.02272727,         inf]], dtype=float32)
>>> pow(a, -1)
array([[ 0.01428571,  0.01923077,  0.07692308,  0.01492537],
       [ 0.01111111,  0.02083333,  0.01754386,  0.03846154],
       [ 0.02325581,  0.02222222,  0.01492537,  0.01123596],
       [ 0.01136364,  0.01538462,  0.02272727,         inf]], dtype=float32)
>>>
>>> inverse_of_a = 1/a
>>> inverse_of_a
array([[ 0.01428571,  0.01923077,  0.07692308,  0.01492537],
       [ 0.01111111,  0.02083333,  0.01754386,  0.03846154],
       [ 0.02325581,  0.02222222,  0.01492537,  0.01123596],
       [ 0.01136364,  0.01538462,  0.02272727,         inf]], dtype=float32)
>>>

Upvotes: 0

thefourtheye
thefourtheye

Reputation: 239503

You can use list comprehension to solve this problem

def question(arr):
    return [[1.0/col if col else col for col in row] for row in arr]

print question([[70,52,13,67],[90,48,57,26],[43,45,67,89],[88,65,44,23]])

To do this in your way

def question(arr):
    new_arr = []
    for i in range(len(arr)):
        row_arr = []
        for j in range(len(arr[i])):
            row_arr.append(arr[i][j] ** -1)
        new_arr.append(row_arr)
    return new_arr

print question([[70,52,13,67],[90,48,57,26],[43,45,67,89],[88,65,44,23]])

You have to create a new list for every row and append it to the result. BTW, ^ refers to bitwise exclusive OR in python. So, you might want to use either 1.0/x form or x**-1 form. ** refers to power, in python.

Upvotes: 4

mxdg
mxdg

Reputation: 314

If you want Python to compute "z^-1", you have to use the math module. The math module has a function, pow, that takes two arguments, and returns base^power. Here's a link to the documentation for pow.

You should replace "z^-1" with "math.pow(z, -1)" and place "import math" at the top.

Upvotes: -1

Related Questions