I'm looking for an efficient way of achieving this, which I think is a slicing-like operation:
>>> mylist = range(100)
>>>magicslicer(mylist, 10, 20)
[0,1,2,3,4,5,6,7,8,9,30,31,32,33,34,35,36,37,38,39,60,61,62,63......,97,98,99]
the idea is: the slicing gets 10 elements, then skips 20 elements, then gets next 10, then skips next 20, and so on.
I think I should not use loops if possible, for the very reason to use slice is (I guess) to do the "extraction" efficiently in a single operation.
Thanks for reading.
but i obtain a list of list :(
Maybe the best way is the straight-forward approach:
I don't think you can avoid the loops.
Edit: Since this is tagged "performance", here a comparison with the modulo solution for
a = range(100)
:[x for x in range(100) if x%30 < 10]
is another way to do it. But, this can be slow as the list size grows.A function on the same lines
result
itertools.compress
(new in 2.7/3.1) nicely supports use cases like this one, especially when combined withitertools.cycle
:Python 2.7 timing (relative to Sven's explicit list comprehension):
Python 3.2 timing (also relative to Sven's explicit list comprehension):
As can be seen, it doesn't make a great deal of difference relative to the in-line list comprehension in 2.7, but helps significantly in 3.2 by avoiding the overhead of the implicit nested scope.
A similar difference can also be seen in 2.7 if the aim is to iterate over the resulting sequence rather than turn it into a fully realised list:
For especially long patterns, it is possible to replace the list in the pattern expression with an expression like
chain(repeat(True, 10), repeat(False, 20))
so that it never has to be fully created in memory.I don't know if you are working with numbers only, but in case you are there is a faster way if you stick to numpy. But the following will only work if you have list that consists of sublists of equal length that were flattened out.
For comparison: