-3

I am looking to search through my array [1,2,3,4,5] representing seconds and check whether my time variable 't' (intially 0) matches a range item, at which point I want to print that time 't' ONLY ONCE.

The loop will be read again but this time NOT print out anything unless another match is found - ie. t is now 2 seconds so when t = 2 then print 't' ONCE.

How can I achieve this in Python? This is what I currently have:

t = 0
dt = 0.05

a = np.arange(1,5,1)

while t < 10:

for x in range(a.size):
    if int(t) == a[x,0]:
        print "TIME IS NOW >>>>>>>", int(t)

t = t+dt

This is working except it is printing the previously matched number again and again until a new matched number is found:

TIME IS NOW >>>>>>>1
TIME IS NOW >>>>>>>1
TIME IS NOW >>>>>>>1
TIME IS NOW >>>>>>>1
TIME IS NOW >>>>>>>1
TIME IS NOW >>>>>>>1
TIME IS NOW >>>>>>>1
TIME IS NOW >>>>>>>1
TIME IS NOW >>>>>>>2
TIME IS NOW >>>>>>>2

and so on.....

I want a RESULT of:

TIME IS NOW >>>>>>>1
-
-(looping but no print out)
TIME IS NOW >>>>>>>2
-
and so on...
Nas25
  • 55
  • 6
  • t is increasing in while loop...t = t +dt (dt = 0.05). Sorry full code not shown - edited. firsdt time posting here.! – Nas25 Nov 21 '16 at 17:07
  • x is the range iteration and int(t) is forcing t as an integer since it is floating point during the loop... – Nas25 Nov 21 '16 at 17:12
  • So, I have a while loop increasing the t in steps of 0.05 up until t = 10. Within this I have a 'for' loop which iterates a given range (my chosen intervals) at which times I want to print the current time 't'. Is it possible and if so how can I achieve this? – Nas25 Nov 21 '16 at 17:47
  • My array is 1,2,3,4,5. I do not have multiple occurances. I believe the loop is currently iterating and just printing out the last match....break / continue does not work... – Nas25 Nov 21 '16 at 17:49
  • I'm no longer using Python - switched to c++ over the years. Thanks – Nas25 Jan 14 '20 at 10:30

3 Answers3

1

you are dealing with floating point numbers here, when you call int in one of then what you get is the floor of that number, that is for a floating point number X.yyyy you get the X regardless of the yyyy, so for example with

0.05 -> 0 
0.55 -> 0
0.95 -> 0 
1.25 -> 1 
etc.

So you get a bunch of 0, then a bunch of 1, and so on...

So how to compare the numbers?? the == would not work because of the precision issue of floats that are always annoying but there is a workaround it, and that is checking how close are the numbers in a certain boundary, that check is simple

abs(a-b) <= epsilon

you choose the epsilon most convenient to the situation, like for example 1e-10 or 1e-16 then the final product is

t = 0
dt = 0.05
check_point = range(1,6)

while t<10:
    if any( abs(t-x) <= 1e-10 for x in check_point ):
        print "TIME IS NOW >>>>>>>", t
    t += dt

and the output is

TIME IS NOW >>>>>>> 1.0
TIME IS NOW >>>>>>> 2.0
TIME IS NOW >>>>>>> 3.0
TIME IS NOW >>>>>>> 4.0
TIME IS NOW >>>>>>> 5.0
Community
  • 1
  • 1
Copperfield
  • 8,131
  • 3
  • 23
  • 29
0

Both of these pieces of code will function, although I am still at a loss, as to why you wish to do this in this way.

This version handles floats to 2 decimal places in the array

import numpy as np
a = np.arange(1,5,1.0)
t=0
while t < 10:
    if round(t,2) in set(a):
        print "TIME IS NOW >>>>>>>", t
        t=round(int(t)+1,2)
    else:
        t+=0.05

This one will handle only integer values

import numpy as np
a = np.arange(1,5,1)
t=0
while t < 10:
    if int(t) in np.unique(a):   #set(a) would work here too
        print "TIME IS NOW >>>>>>>", int(t)
        t=int(t)+1 #increment to next integer
    t+=0.05
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
0

Many thanks for the answers ,especially 'Copperfield' - your epsilon boundary check lead me to find out more about the issue of floating point numbers and although your solution didn't work for me directly I found a command using Numpy (since my array was a Numpy array) - the 'isclose' command. This seems to do what your solution does and checks the list items against a comparison using a tolerance.

The correct solution for my issue was:

if np.isclose(insRate, t, atol=0.0001).any():
    print "TIME IS NOW >>>>>>>", t

Thanks again everyone!

Nas25
  • 55
  • 6