Python List Maker

Lets manipulate some lists. Go on, it’ll be fun!

mystr = "1-2, 3, 5, 7-9, 12-20"
finallist = []

templist = mystr.split(",") # explode

for z in range(len(templist)): # remove whitespace
    templist[z] = templist[z].strip()


for x in range(len(templist)):
    if templist[x].isdigit():
        finallist.append(int(templist[x]))
    else:
        span = str(templist[x]).split("-")
        for y in range(int(span[0]), (int(span[1]) + 1)):
            finallist.append(y)

print(finallist)

This script takes a list written in a human readable (and, more to the point, easily human-writeable) format such as “1-2, 3, 5, 7-9, 12-20” and converts into a list (a kind of python array) for use by other functions.

I have implemented this into my JADOS program to good effect.

Advertisements

About Mr Chimp

I make music, draw pictures, browse the internet, programme, and make sweet, sweet cups of tea until the early hours.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s