samedi 5 juin 2021

Is there any possibility to Merage element in sublist with another one by using certain condition?

merge element of sublist with another sublist without duplicate


I am working on solving the vehicle routing problem and get the initial best solution and I want to be exploited and need to get rid of duplicate nodes while merging them. my problem I want to merge the sublist of element [8] with others that carry the condition equal to 3 elements, but cause there are two sets consist 3 elements like [18, 22, 34, 8], [35, 36, 37, 8] and I need integrate element 8 in one of them randomly

bestsolution= [[22, 15, 20, 2, 32, 30, 4, 17], [27, 8, 9, 14, 33, 21, 5, 13], [26, 28, 6, 31, 11], [18,22,34],[35,36,37],[8]]                              
for a in bestsolution:
    if len(a)==1: 
        p=a 
        del bestsolution[-1]         
        for b in bestsolution:  
            if len(b)==2: 
                b.extend(p) 
                print("p",b)
                print("bestsolution1-2",bestsolution)
            elif len(b)==3:
                b.extend(p) 
                print("p",b)
                print("bestsolution1-3",bestsolution)    
my results:
bestsolution1-3 [[22, 15, 20, 2, 32, 30, 4, 17], [27, 8, 9, 14, 33, 21, 5, 13], [26, 28, 6, 31, 11], [18, 22, 34, 8], [35, 36, 37, 8]]

'''

code targeted:
    bestsolution1-3 [[22, 15, 20, 2, 32, 30, 4, 17], [27, 8, 9, 14, 33, 21, 5, 13], [26, 28, 6, 31, 11], [18, 22, 34], [35, 36, 37, 8]]
'''

Thank you

Aucun commentaire:

Enregistrer un commentaire