Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use parseIntSet from <a href="http://thoughtsbyclayg.blogspot.com/2008/10/parsing-list-of-numbers-in-python.html" rel="noreferrer">here</a></p> <p>I also like the pyparsing implementation in the comments at the end.</p> <p>The parseIntSet has been modified here to handle "&lt;3"-type entries and to only spit out the invalid strings if there are any.</p> <pre><code>#! /usr/local/bin/python import sys import os # return a set of selected values when a string in the form: # 1-4,6 # would return: # 1,2,3,4,6 # as expected... def parseIntSet(nputstr=""): selection = set() invalid = set() # tokens are comma seperated values tokens = [x.strip() for x in nputstr.split(',')] for i in tokens: if len(i) &gt; 0: if i[:1] == "&lt;": i = "1-%s"%(i[1:]) try: # typically tokens are plain old integers selection.add(int(i)) except: # if not, then it might be a range try: token = [int(k.strip()) for k in i.split('-')] if len(token) &gt; 1: token.sort() # we have items seperated by a dash # try to build a valid range first = token[0] last = token[len(token)-1] for x in range(first, last+1): selection.add(x) except: # not an int and not a range... invalid.add(i) # Report invalid tokens before returning valid selection if len(invalid) &gt; 0: print "Invalid set: " + str(invalid) return selection # end parseIntSet print 'Generate a list of selected items!' nputstr = raw_input('Enter a list of items: ') selection = parseIntSet(nputstr) print 'Your selection is: ' print str(selection) </code></pre> <p>And here's the output from the sample run:</p> <pre><code>$ python qq.py Generate a list of selected items! Enter a list of items: &lt;3, 45, 46, 48-51, 77 Your selection is: set([1, 2, 3, 45, 46, 77, 48, 49, 50, 51]) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload