Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

What I have tried:

Python
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)<pre>


thanks in advance for any help
Posted
Updated 28-Oct-22 20:23pm
v3

1 solution

You need to define the rules for changing. At a guess from looking at your desired results you need to check each entry in the first array. Then find all values in base array larger than its largest number. Create the new three element array from that and continue until all have been processed.

Try this:
Python
change_array = [1,2],[1,3],[1,4],[2,3],[2,4]
base_array = [1,2,3,4]
save_array = []
for ca in change_array:     # loop through all entries
    highest = ca[1]         # get the last value in the item
    for bav in base_array:  # check all base values
        if bav > highest:   # if it is higher 
            cp = ca[:2]     # take a copy of the original item
            cp.append(bav)  # append the new highest number
            save_array.append(cp) # save the modified element
print(save_array)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900