Discovering Python’s Itertools

Discovering the Python Standard Library – itertools

I thought this was very cool; while reading through stackoverflow, and I came across answers using the intertools module

You can take two lists:

x = [5, 2, 7, 4, 6]

y = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

and you can combine these lists:

coords = [(x, y) for (x, y) in zip(x, y)]

or, you can sort a list, and then map it onto the second list (i.e. sort the second list based on the frequency of the first)

freq = [(x, y) for (x, y) in sorted(zip(x, y))]    # sorts list x (the frequency), and returns the coords (x, y) in the sorted order of x

# i.e. output –>  (2, ‘b’), (4, ‘d’), (5, ‘a’), (6, ‘e’), (7, ‘c’)


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s