OLW: #Python Subsets of Set

The following nifty #oneliner uses #functional #programming concepts available in Python to return all subsets of an array argument:

f = lambda l: reduce(lambda z, x: z + [y + [x] for y in z], l, [[]])

That is, f([1, 1, 1, 4, 1, 10, 4, 1, 4, 10]) will return [[],[1],[4],[10],[1,4],[1,10],[4,10],[1,4,10]]

“I was born at a very young age.”  -Groucho Marx

Advertisements

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