ShanZhengYang
ShanZhengYang

Reputation: 17621

Determine if there is at least one zero in a multidimensional numpy array

I have the following code: There exists a numpy array multidimensional_array which has either has all integers and no zeros, or one zero among many integers:

zeros_list = []   

for line in multidimensional_array:   # if find any zeros, append to list 'zeros'
    for x in line:
        if x.any() == 0:
            zeros_list.append(x)
        else:
            pass

for item in zeros:
    if item == 0:
        sys.stdout.write( 'True')   # if there is a zero, True
    else:
        sys.stdout.write( 'False')  # otherwise, False

Unfortunately, this doesn't run correctly. If there's a zero, it outputs True. If not, nothing happens. Each time I run this within a python script script.py, it should reset. How can I set this to run 'False'?

Upvotes: 11

Views: 10254

Answers (5)

John La Rooy
John La Rooy

Reputation: 304117

>>> import numpy as np
>>> A = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> (A==0).any()
False
>>> (A!=0).all()
True
>>> 0 not in A
True
>>> A = np.array([[1,2,3],[4,5,6],[7,0,9]])
>>> (A==0).any()
True
>>> (A!=0).all()
False
>>> 0 not in A
False

Your final for loop should just be an if

if zeros:
    sys.stdout.write('True')   # if there is a zero, True
else:
    sys.stdout.write('False')  # otherwise, False

Upvotes: 5

abe
abe

Reputation: 504

To add

import sys
zeros_list = []
string1 = input() #If you received the string this way, below code is valid. 

for line in string1:   # if find any zeros, append to list 'zeros'
    for x in line:
        if x == '0':#Here you should not check for digits when ciphering a string. Unless you put int(item) which could cause a TypeError
            zeros_list.append(x)
        else:
            pass

for item in zeros_list:
    if item == '0': #Here you should not check for digits when ciphering a string. Unless you put int(item) which could cause a TypeError
        sys.stdout.write( 'True')   # if there is a zero, True
    else:
        sys.stdout.write( 'False')  # otherwise, False`

And:

for item in zeros: #Did you mean zeros_list?

End note, any() is not a builtin Python function, where did this come about? Please include all the code necessary to run your code.

I stand corrected, any() is a useful function :D


Just so you know, 0 in Python as a boolean is False.

    if item == 0:

In the second for loop could have a different outcome than what you are expecting.

Upvotes: 0

timgeb
timgeb

Reputation: 78650

I am sorry. It is a [multidimensional] numpy array. Is there or is there not one zero in a numpy array? That's the test

Alright, that will get us someplace. You can simply issue

0 in multidimensional_array

Demo:

>>> import numpy as np
>>> test1 = np.arange(6).reshape(2,3)
>>> test1
array([[0, 1, 2],
       [3, 4, 5]])
>>> 0 in test1
True
>>> test1[0][0] = 42
>>> test1
array([[42,  1,  2],
   [ 3,  4,  5]])
>>> 0 in test1
False

Upvotes: 19

lcieslak
lcieslak

Reputation: 91

If you append zeros_list then is should be:

for item in zeros_list:

Upvotes: -1

letsc
letsc

Reputation: 2567

Since you said s is a string, a MUCH easier wasy would be to use string.count()

>>> s = '112312390'
>>> s.count('0')
1
>>> s = '11231239'
>>> s.count('0')
0
>>>

Upvotes: 3

Related Questions