Reputation: 115
I'm trying to figure out the best way to check a variety of ranges of a list algorithmically and drawing a blank. Here is what the ranges it should check should look like.
#first it checks the whole list, then
foo[1:]
foo[:-1]
foo[2:]
foo[1:-1]
foo[:-2]
foo[3:]
foo[2:-1]
foo[1:-2]
foo[:-3]
#etc, for as long as the ranges will fit in the list
The first and last steps are easy enough - essentially I could just use
foo[iter:]
foo[:-iter]
But the intermediate ones are where I'm not sure how to go about it.
Upvotes: 1
Views: 51
Reputation: 13779
This works, although at first glance it is slightly inelegant due to the magic-number ranges.
def decreasing_ranges(seq):
for length in range(len(seq), 0, -1):
for start in range(len(seq) - length, -1, -1):
yield foo[start:start + length]
Upvotes: 1
Reputation: 656
map(lambda x: foo[x[0]:-x[1]], itertools.permutations(range(1, 4), 2))
Upvotes: 2