dreamzboy
dreamzboy

Reputation: 841

Iterate Through a Non-Sequential Range

I would like to iterate through a non-sequential range. In another words, skipping certain indexes/elements based on user input like so.

>>> 0
>>> 3
>>> 4
>>> 6
>>> 7
>>> 8
>>> 9

I couldn't figure out how so I attempted this method.

import re

>>> crazyrange = '0, 3-4, 6, 7-9'    # User input string
>>> crazyrange = re.split (', ', crazyrange)
>>> crazyrange
['0', '3-4', '6', '7-9']
>>>
>>> masterlist = []
>>> def get_range (values):
    rangelist = []
    for i in range (int (values [0]), int (values [1]) + 1):
        rangelist.append (i)
    return rangelist
>>>
>>> for i in crazyrange:
    if '-' in i:
        values = i.split ('-')
        masterlist.extend (get_range (values))
    else:
        masterlist.append (int (i))
>>>
>>> masterlist
[0, 3, 4, 6, 7, 8, 9]

Is there a better way to do this?

Upvotes: 1

Views: 1581

Answers (2)

Matthias Fripp
Matthias Fripp

Reputation: 18625

Your code looks pretty good, but you don't need re and you could tidy it up a bit by using python's range function and yielding the items from a generator function:

crazyrange = '0, 3-4, 6, 7-9'    # User input string

def get_range(range_string):
    items = range_string.replace(" ", "").split(",")
    for i in items:
        if '-' in i:
            start, end = i.split('-')
            for j in range(int(start), int(end)+1): # range() is not inclusive
                yield j
        else:
            yield int(i)

for x in get_range(crazyrange):
    print x

print list(get_range(crazyrange))
# [0, 3, 4, 6, 7, 8, 9]

There's also an option without explicit loops:

crazyrange = '0, 3-4, 6, 7-9'    # User input string
import itertools
items = [i.split("-") for i in crazyrange.split(", ")]
range_list = [range(int(i[0]), int(i[-1])+1) for i in items]
print list(itertools.chain(*range_list))

Or this option may be the simplest:

crazyrange = '0, 3-4, 6, 7-9'    # User input string

def get_range(item):
    pair = item.split('-')  # may only be one item, but that's OK
    return range(int(pair[0]), int(pair[-1])+1)

items = crazyrange.split(', ')
print [i for item in items for i in get_range(item)]
# [0, 3, 4, 6, 7, 8, 9]

Upvotes: 2

Chiheb Nexus
Chiheb Nexus

Reputation: 9257

You can have the same output within less code using list comprehension:

crazyrange = '0, 3-4, 6, 7-9'

ranges = [range(int(k.split("-")[0]), int(k.split("-")[-1]) +1) for k in crazyrange.split(',')]

print(ranges)
print([j for k in ranges for j in k])

Output:

[range(0, 1), range(3, 5), range(6, 7), range(7, 10)]
[0, 3, 4, 6, 7, 8, 9]

Upvotes: 1

Related Questions