Rogue258
Rogue258

Reputation: 135

Writing the function to get even numbers from an array and append them to a different array in Python Numpy

Need to write a function which takes a dynamic array as variables. The function then checks for all even numbers in the array and appends those even numbers to a different array. Also any help on the time for computing this code would be appreciated.

I have defined a function and initiated an array. I am trying do it in Numpy. I tried reading on any() and all() but don't know how to implement it as I want to perform an iterative loop with appending.

def even(x):
        b = []
        a = [x]
        for x in a:
            if x % 2 == 0:
                b = b.append(x)
                next 
arr1 = np.arange(10,51)
even(arr1)


ValueError                                Traceback (most recent call last)
<ipython-input-10-302372375b1e> in <module>
      7                 next
      8 arr1 = np.arange(10,51)
----> 9 even(arr1)

<ipython-input-10-302372375b1e> in even(x)
      3         a = [x]
      4         for x in a:
----> 5             if x % 2 == 0:
      6                 b = b.append(x)
      7                 next

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Don't understand the first error output's meaning. For the second one I do understand that the array output is boolean and therefore need to use any or all. However, what I want is that the output of array a be integer number and if it meets the for criteria of even then it gets added to array b.

Edit: Thanks for the answers all of you. Definitely helps with the solution. However, just a minor query, if someone could explain what is the meaning of the first error.

I am learning python and this is for educational purposes.

Upvotes: 0

Views: 2505

Answers (4)

sandip kumar panda
sandip kumar panda

Reputation: 1

complete_array = [*range(1,21)]
print (complete_array)
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
even_array = [i for i in a if i%2==0]
print (f'Required Array: {even_array}')
>>> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Upvotes: 0

hiro protagonist
hiro protagonist

Reputation: 46921

the problem with your code is that numpy arrays broadcast; x % 2 == 0 will evaluate to an array:

[ True False  True False  ... True]

which does not evaluate to a simple bool in your if statement: bool(arr1 % 2 == 0) will rasie the ValueError you got.

instead of your function you could just use boolean or 'mask' indexing:

import numpy as np

arr1 = np.arange(10, 51)
res = arr1[arr1 % 2 == 0]
# [10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50]

Upvotes: 1

basilisk
basilisk

Reputation: 1277

import numpy as np   

 def even(your_array):
        arr =np.array([n for n in your_array if n % 2 == 0])

you can also use the filter function:

  def even(your_array):
        return np.array(list(filter(lambda n: n % 2 == 0, your_array)))

when you call the function you ll get a new array:

arr = even(some_array)

Upvotes: 0

Asnim P Ansari
Asnim P Ansari

Reputation: 2497

You can use python filter function and simplify your function definition as follows

def get_even(arr1):
    return np.array(list(filter(lambda x:x%2==0,arr1)))

Upvotes: 0

Related Questions