given two arrays I'm trying to get all the different combinations of the arrays with no repeats of sequences and without using itertools. My problem comes when I try to combine a 2 dimensional array with a 1 dimensional array. given arrays [1,2],[1,3],[1,4],[2,3],[2,4] and [1,2,3,4] I want it to yield [1,2,3],[1,2,4],[1,3,4],[2,3,4]. currently it will produce [1,2,3], [1,2,4] and [1,3,4] but not [2,3,4] and I can't figure out why.
change_array = [1,2],[1,3],[1,4],[2,3],[2,4]
base_array = [1,2,3,4]
save_array = []
def candidate_generation2(change_array, base_array, save_array):
current_iteration = []
for x in range(0, len(change_array)):
d = []
for y in range(0, len(change_array[0])):
d.append(change_array[x][y])
for z in range(x 1, len(base_array)):
e = d.copy()
tag = 0
for a in range(0, len(e)):
if e[a] == base_array[z]:
tag = 1
if tag == 0:
e.append(base_array[z])
save_array.append(e)
current_iteration.append(e)
chenge_array = current_iteration.copy()
candidate_generation2(change_array, base_array, save_array)
print(save_array)
thanks in advance for any help
CodePudding user response:
change_array = [1,2],[1,3],[1,4],[2,3],[2,4]
base_array = [1,2,3,4]
def candidate_generation2(change_array, base_array):
for array in change_array:
for value in base_array[base_array.index(array[-1]) 1:]:
yield array [value]
for value in candidate_generation2(change_array, base_array):
print(value)
CodePudding user response:
Using list comprehension
change_array = [1,2],[1,3],[1,4],[2,3],[2,4]
base_array = [1,2,3,4]
save_array = []
[save_array.append(sorted(i [j])) for j in base_array for i in change_array if j not in i and sorted(i [j]) not in save_array]
print(save_array)
Output:
[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]