10

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'?

timgeb
  • 76,762
  • 20
  • 123
  • 145
ShanZhengYang
  • 16,511
  • 49
  • 132
  • 234

5 Answers5

16

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
timgeb
  • 76,762
  • 20
  • 123
  • 145
5
>>> 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
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • `(A==0).any()` should be the preferred option, as I will return on the first 0, rather than `.all()` which must scan the whole array. – Benjamin Dec 15 '15 at 02:11
  • @Benjamin, `.all` will also stop at the first `0` But I think either of those expressions will completely create the `(A==0)` array which the `in` version won't need to do. – John La Rooy Dec 15 '15 at 02:24
  • @JohnLaRooy, yes, indeed, my mistake. I was thinking of a different case and jumped to conclusions. – Benjamin Dec 15 '15 at 02:26
3

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
>>>
letsc
  • 2,515
  • 5
  • 35
  • 54
0

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.

abe
  • 504
  • 4
  • 13
-1

If you append zeros_list then is should be:

for item in zeros_list:
lcieslak
  • 91
  • 6