python loop only print values once
Date : March 29 2020, 07:55 AM
this one helps. I think you have the idea right in the if measurement < 500: condition, printing "light" only when last was different. You just have to repeat similar logic in the > 500 condition. But the real problem is that last here is a local variable, so the value will get reset to 9 on every call. So you need to remove the last=9, and define last outside of the function and declare it as global inside the function: #in main program
last = 9
def RCTime ...:
global last
....
|
MXNet print intermediate symbol values
Date : March 29 2020, 07:55 AM
seems to work fine How do i find the actual numerical values held in an MXNet symbol. , After looking around a bit, I found you can do this by: x = mx.sym.Variable('x')
y = mx.sym.Variable('y')
z = x + y
executor = z.bind(mx.cpu(), {'x': mx.nd.array([100,200]), 'y':mx.nd.array([300,400])})
output = executor.forward()
[<NDArray 2 @cpu(0)>]
print output[0].asnumpy()
array([ 400., 600.], dtype=float32)
|
how do i print 6 values per line using the while loop statement in python
Date : March 29 2020, 07:55 AM
like below fixes the issue here's my code. i need some help in figuring out the print function. , In python, there is always a short way to do something : arr = [format(x, '7d') for x in range(40, 999, 40)]
print('\n'.join(''.join(arr[i:i+6]) for i in range(0, len(arr), 6)))
40 80 120 160 200 240
280 320 360 400 440 480
520 560 600 640 680 720
760 800 840 880 920 960
|
Functional way to convert a loop that is based on intermediate value that updates on each loop in python
Tag : python , By : PeteFilicetti
Date : March 29 2020, 07:55 AM
around this issue Not dwelling on why you want to do this, there are at least two different approaches to this. It very much depends on whether you want the call to be a tail call (which allows for the compiler to optimize away the function call in many strict functional languages - though not Python) or if you want a more straight forward recursive function. The straight forward implementation is import numpy as np
def recursive(rs, n_extra_pos, irrel_pos):
if n_extra_pos == []:
return [], irrel_pos
extra_pos, irrel_pos = recursive(rs, n_extra_pos[:-1], irrel_pos)
sample = set(rs.choice(list(irrel_pos), n_extra_pos[-1], replace=False))
return extra_pos + [sample], irrel_pos - sample
irrel_pos = set(range(3, 11))
n_extra_pos = [2, 3]
rs = np.random.RandomState(1)
extra_pos, irrel_pos = recursive(rs, n_extra_pos, irrel_pos)
print(extra_pos, irrel_pos)
import numpy as np
def tail_recursive(rs, n_extra_pos, extra_pos, irrel_pos):
if n_extra_pos == []:
return extra_pos, irrel_pos
sample = set(rs.choice(list(irrel_pos), n_extra_pos[0], replace=False))
return tail_recursive(rs, n_extra_pos[1:],
extra_pos + [sample], irrel_pos - sample)
irrel_pos = set(range(3, 11))
n_extra_pos = [2, 3]
rs = np.random.RandomState(1)
extra_pos, irrel_pos = tail_recursive(rs, n_extra_pos, [], irrel_pos)
print(extra_pos, irrel_pos)
|
Loop over list in Angular and print intermediate heading, reusing structure
Date : March 29 2020, 07:55 AM
|