Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import combinations
- # Function 1 <----> Generate all the sublists of a list
- # If list = [1, 2, 3], there are 8 sublists:
- # [], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]
- def subLists(myList):
- N = len(myList)
- returnedList = list()
- returnedList.append([])
- for remaining in range(1, N):
- combinationsOfIndecesRemaining = list(combinations(list(range(N)), remaining))
- for comb in combinationsOfIndecesRemaining:
- sublist = list()
- for i in range(len(comb)):
- sublist.append(myList[comb[i]])
- returnedList.append(sublist)
- returnedList.append(myList)
- return returnedList
- # MAIN FUNCTION
- print()
- l = [0, 1, 2, 3, 4]
- sub = subLists(l)
- print("List = " + str(l))
- print("Sublists = " + str(sub))
- print("There are " + str(len(sub)) + " = 2^" + str(len(l)) + " sublists of the list " + str(l))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement