-1

I want to turn every 0 of a large array list into a -1. This has do be done as quickly as possible. A for loop is very slow for me. My array is a numpy array. Does you know a faster solution for this simple problem?

Here is a example code:

test_array=[1 0 0 1 0 0 1 1 0 1 1 0 1 0 0 0]

for index, value in enumerate(test_array):
if value == 0:
    l[index] = -1

test_array=[1 -1 -1 1 -1 -1 1 1 -1 1 1 -1 1 -1 -1 -1]

My real list is a lot longer than the one in this example, so a quick solution is a performance factor.

  • 3
    please specify whether these are actual arrays (i.e. numpy arrays) or just lists. Your question is ambiguous otherwise. – amdex Jul 24 '19 at 07:27
  • 1
    Please include some actual code in your question. Currently it doesn't run when copied. – MSeifert Jul 24 '19 at 07:34
  • Possible duplicate of https://stackoverflow.com/questions/19666626/replace-all-elements-of-python-numpy-array-that-are-greater-than-some-value / https://stackoverflow.com/questions/19766757/replacing-numpy-elements-if-condition-is-met – MSeifert Jul 24 '19 at 07:38

3 Answers3

2
In [1]: a = np.random.randint(0,2,1000000)  

In [2]: a                                                                      
Out [2]: array([0, 1, 1, ..., 1, 1, 0])

In [3]: %timeit a[a==0]=-1                                                     
1.73 ms ± 50.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
scrx2
  • 2,242
  • 1
  • 12
  • 17
  • If you include the timings it would be more meaningful if you compared them to the original approach (or the approaches in the other answers). – MSeifert Jul 24 '19 at 07:40
  • Time testing in this way is inappropriate. The values of `a` are changed in the first testing call and remain the same afterwards. – GZ0 Jul 24 '19 at 07:46
1

try:

import numpy as np
array1 = np.array([1,0 ,0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0])
print (np.where(array1 == 0, -1, array1))

output:

[ 1 -1 -1  1 -1 -1  1  1 -1  1  1 -1  1 -1 -1 -1]
ncica
  • 7,015
  • 1
  • 15
  • 37
1

If your test_array is actual list

test_array = [1,0 ,0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0]
test_array_New = []
for i in test_array:
     if i != 0:
             test_array_New.append(i)
     else:
             test_array_New.append(-1)
test_array_New

Output:

[1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1]
kashiff007
  • 376
  • 2
  • 12
  • 1
    Even if the input is a list, can this method even outperform the solution in the original post? – GZ0 Jul 24 '19 at 07:53
  • I thought to give the basics about how to read the list with simple looping. I have tried this with python2.7 and its working. It will slower process than zip and enumerate for sure. – kashiff007 Jul 24 '19 at 08:26
  • The OP was clearly asking for a solution that performs better than the provided solution, rather than just another solution. That being said, this solution could be made faster by employing basic optimization techiques such as list comprehensions. – GZ0 Jul 24 '19 at 08:35