Python script gives multiple outputs, if/else sys.out
Python script gives multiple outputs, if/else sys.out
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'?
Answer by letsc for Python script gives multiple outputs, if/else sys.out
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 >>>
Answer by Ibrahim Ahmed for Python script gives multiple outputs, if/else sys.out
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.
Answer by Łukasz for Python script gives multiple outputs, if/else sys.out
If you append zeros_list then is should be:
for item in zeros_list:
Answer by John La Rooy for Python script gives multiple outputs, if/else sys.out
>>> 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
Answer by timgeb for Python script gives multiple outputs, if/else sys.out
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
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71
0 comments:
Post a Comment