0

I have a while loop that runs through a fixed step and constantly checks if a value in an array is not equal to the while loop counter, and prints that number:

import numpy as np

values = [60.0, 75.8, 85.0, 90.0]
values = np.asarray(values)
counter = 50
while counter <= 100:
    closest = (np.abs(values - counter)).argmin()
    if (values[closest] > (counter - 1) and values[closest] <counter):
        print("we skipped a value " + str(values[closest]))
    counter = counter + 1

I wrote this code which has the mentioned functionality but it doesn't seem very elegant or efficient. Is there a quicker way to find these skipped values in Python?

7
  • 1
    It seems like you are just checking if the number is a whole number, no? And if it is inside the range of [50, 100]? Commented Feb 24, 2020 at 14:56
  • No , if the values are equal to any of the counter values then we good , if not we skipped one value , the jump between counters could also be 0.1 its a global variable Commented Feb 24, 2020 at 14:59
  • Why does your code not need to print that we skipped a value if the counter is equal to 70 for example? 70 would be in the loop counter, but it is not in your array. Commented Feb 24, 2020 at 15:05
  • What is your criteria for "skipping" a value? It seems to be just counter > values[closest] > counter - 1, which is only satisfied for numbers that aren't integer values. If you don't want to skip a value, why don't you directly iterate on the values? Commented Feb 24, 2020 at 15:33
  • 1
    It doesn’t really matter whether you are looking for multiples of 1 (integers) or something else. Point is, you can calculate this easily (e.g. using modulo). The question is what is your goal that a better solution must still satisfy? Do you just want to find the indices (i.e. can we just calculate them)? Do you want to do something (i.e. do you need the loop)? Do you want to do something with the values (i.e. do you need another kind of loop)? Commented Feb 25, 2020 at 7:25

2 Answers 2

1

This avoids looping and prints a list of skipped values:

print("skipped values: " + str(values[np.isin(values, range(50, 101), invert=True)]))

numpy.isin returns values from the first argument that are in the second argument. Setting invert to True means return everything not in the second argument.

Sign up to request clarification or add additional context in comments.

Comments

1

It's hard to tell from the question exactly what you need, but this produces the same output:

for v in sorted(set(values).difference(range(50, 101))):
    print("we skipped a value " + str(v))

It creates a set from the values, removes every element between 50 and 100 inclusive, then prints any remaining values in ascending order.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.