python - Generationg a list from user-input dynamically -
as title says, i'm processing command-line options create list user input, this: "3,28,2". got far:
>>> rr = "3,28,2" >>> rr = re.split(r"[\w]+", rr) >>> map(int, xrange( int(rr[0]),int(rr[1]),int(rr[2]) )) [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27] >>> fyi, re.split() because users allowed use comma (,) or space or both @ same time delimiter.
my question how can "automate" xrange(object) bit user-input can or without start , step value (i.e. "3,28,2" vs. "3,28" vs. "28"). len(rr) tell me number of elements in input i'm kind of lost here how can use information write xrange/range part dynamically.
any idea(s)? trying make code efficient possible. so, advise on appreciated. cheers!!
try this:
>>> rr = "3,28,2" >>> rr = re.split(r"[\w]+", rr) >>> map(int, xrange(*map(int, rr))) [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27] >>> the * unpack elements arguments xrange.
Comments
Post a Comment