0

Hello, I am new to Python and would be verry happy if someone would give me the code for a function that yields all sublists of a list. I need it for later implemetation in C, where I have to do work on these sublists.

I would need something like this:

def printSubs(l):
    for p in sublists(l):
        print p        # just for test

def sublists(l):
    # now this is the part I can't figure out :(

So the result of printSubs([1,2,3])
would be as follows
[1]
[2]
[3]
[1,2]
[1,3]
[2,3]
[1,2,3]

The order is not important!

Thanks for any help!

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by woooee
0

"So the result of printSubs([1,2,3])"
You would increment a counter or use a for() loop in the range of 0-->len(list_passed) to append an increasing number of items to the "sublist_list" based on another counter or for() loop. So the first pass would append one/each individual item, the second pass would append items 0 and 1, then 1 and 2, etc., the third pass would append 3 items each, until this counter is greater than the length of the list. Perhaps someone has a more elegant solution. This brute force method will work, and if the list isn't huge, won't take all that long.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.