I am creating a nested list using the following code:
from itertools import combinations
def get_nested_list(arr, n, k):
iterator = 0
nest = []
for i in range(1, n):
for combo in combinations(arr, i):
odd_num = 0
for item in combo:
if item % 2 != 0:
odd_num += 1
if odd_num <= k:
iterator += 1
nest.append(combo)
nest = [list(i) for i in nest]
return nest
a = [1, 2, 3, 4]
b = len(a)
c = 1
print(get_nested_list(a, b, c))
This is my output:
[[1], [2], [3], [4], [1, 2], [1, 4], [2, 3], [2, 4], [3, 4], [1, 2, 4], [2, 3, 4]]
However, I got elements like [1,2]
and [1,4]
that have the same x value, [2, 4]
and [3, 4]
that have the same y value. How can I create a list with completely distinct sub-lists? This is what I'm trying to accomplish:
[[1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [2, 3, 4]]