Parsing numeric ranges
alain at dpt-info.u-strasbg.fr
Fri Feb 25 10:44:13 CET 2011
Seldon <seldon at katamail.it> writes:
> I have to convert integer ranges expressed in a popular "compact"
> notation (e.g. 2, 5-7, 20-22, 41) to a the actual set of numbers (i.e.
What form does the input have? Are they strings, or some other
> Is there any library for doing such kind of things or I have to write
> it from scratch ?
What kind of result do you need? An explicit list? A generator?
Here is a naive solution where the input is a list of strings and the
result a generator:
for item in l:
bounds = item.split('-')
if len(bounds) == 1:
elif len(bounds) == 2:
for v in range(int(bounds),1+int(bounds)):
pass # ignore, or throw, or...
# Use as in:
for x in values(["1","2-3","4-10"]):
More information about the Python-list