0

I am making a while loop counter that i want to skip a certain numbers and those number's value+10, +20, +30...and so on. Here's what i have been trying so far:

lstLength = 26
skipNum = [2, 8]

lst = []
count = 0
while len(lst) < lstLength:
    count +=1
    if count == skipNum[0] or count == skipNum[1]:
        continue
    lst.append(count)
print(lst)

I would like the result list to be lstLength long while skipping numbers 2, 8, 12, 18, 22, 28, 32, 38 and so on....How can i do that? Thank you!

3 Answers 3

5

Use:

if count % 10 in skipNum:
    continue

% is the modulus operator, so count % 10 is the last digit of count.

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

Comments

0

Just for comparison's sake, here's a way to implement this using itertools functions, which ends up being faster than the while loop method:

from itertools import count, ifilter, islice

def iter_filt(black_list, lstLen):
    return list(islice(ifilter(lambda x: x % 10 not in black_list, count(1)), lstLen))

Usage:

>> print(iter_filt([2,8], 26))
[1, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 17, 18, 19, 20, 21, 23, 24, 25, 27, 28, 29, 30, 31, 33]

The function works by starting with count(1), which will generate an infinite series of numbers starting from 1 (1,2,3,4,5,6,...). It then runs that through ifilter, which filters out any results where x % 10 in black_list (as suggested by Barmar). Then islice is used to only take the first lstLen results from the ifiltered count iterator.

Doing it this way is a little faster than the while loop approach:

from itertools import count, islice, ifilter
import timeit

def while_filt(skipNum, lstLength):
    lst = []
    count_ = 0 
    while len(lst) < lstLength:
        count_ +=1 
        if count_ % 10 in skipNum:
            continue
        lst.append(count_)
    return lst 



def iter_filt(black_list, lstLen):
    return list(islice(ifilter(lambda x: x % 10 not in black_list, count(1)), lstLen))


if __name__ == "__main__":
    black_list = [2,6]
    length = 1000026
    print timeit.timeit("while_filt(%s,%s)" % (black_list, length),
                  setup="from __main__ import while_filt", number=50)
    print timeit.timeit("iter_filt(%s,%s)" % (black_list, length),
                  setup="from __main__ import iter_filt", number=50)

Output:

dan@dantop2:~$ ./funtimes.py 
15.1266388893  # while_filt
11.8498108387  # iter_filt

Comments

0

Here's my attempt. It will populate your list until it hits a length of 25. It will use the values in skipNum to ignore values 2, 8 as well as 2+10, 2+20, ... and 8+10, 8+20, ...

lstLength = 26
skipNum = [2, 8]

count = 0
lst = []
while len(lst) < lstLength:
  count += 1

  if count in skipNum:
    continue

  #check if count is equal to 2+10, 2+20, ... or 8+10, 8+20, ... etc. If
  #that's the case, `add` will be `False`

  #map to a list of booleans indicating whether `count = skipNum+(n*10)` for any `n`
  bool_arr = map(lambda x, y: ((count - y) % 10 != 0), skipNum)

  #reduce array of booleans to a single boolean value indicating we can `add`
  add = not any(bool_arr)

  if add:
    lst.append(count)


print(lst)

4 Comments

Sure, I'll try when I get home later.
@Barmar The docs for reduce are here: docs.python.org/2/library/functions.html#reduce. Basically, it applies the lambda function to each element in skipNum, using the previous result as the x parameter (and using the third parameter, True, for x on the first iteration). So basically, if (count - y) % 10 == 0 for either element in skipNum, the reduce call ends up returning False, and the item isn't added to the list. This works, but ends up being much slower than your solution.
I know what reduce does, but this is a somewhat unusual use of it, that's why I asked you to add an explanation to the answer.
@Barmar: I broke the reduce into two operations to make it a little clearer.

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.