0

Im trying to find a list front_design_array in which sum of every element equals to 10. Starting with array and f_ext_ele_groups. In each iteration, the elements in array is appended in f_ext_ele_groups. For exapmle, after first iteration, f_ext_ele_groups would turn into

[[1,1], [1,2],[1,3]...[1,9], [2,1], [2,2]......[2,8]......[3,1], [3,2]......[3,7]...............[9,1]]

At the end, front_design_array should contain, for example, [1,1,1,1,1,1,1,1,1,1] (its sum is 10) and [3,3,3,1]. After very iteration, the elements of f_ext_ele_groups is controlled for any elements whether equals to 10. If true, it is appended to front_design_array.

But front_design_array eventually just contained

[[10], [1, 1, 2, 3, 1, 2], [1, 1, 2, 3, 1, 2], [2, 1, 2, 3, 1, 1], [3, 1, 2, 3, 1], [3, 1, 2, 3, 1], [4, 1, 2, 3], [4, 1, 2, 3], [4, 1, 2, 3], [6, 1, 2, 1], [7, 1, 2], [7, 1, 2], [9, 1], [1, 1, 2, 3, 1, 2], [1, 1, 2, 3, 1, 2], [2, 1, 2, 3, 1, 1], [2, 1, 2, 3, 1, 1], [3, 1, 2, 3, 1], [5, 1, 2, 1, 1], [5, 1, 2, 1, 1], [6, 1, 2, 1], [8, 1, 1]] 

and doesnt contain for example [1,1,1,1,1,1,1,1,1,1] (its sum is 10) and [3,3,3,1] (its sum is 10).

Where is my fault?

Here is my code:

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
f_ext_ele_groups= [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]
                              
front_design_array=[]          
length=len(f_ext_ele_groups)

while length > 0:               

    draft= []
    
    for x in f_ext_ele_groups:  
                                
        sumation= sum(x)
        
        if sumation ==10:
            front_design_array.append(x)
    
        elif sumation < 10:
            
            for ele in array:
                var=x  
                sum_t=int(sumation+ ele)
  
                if sum_t <= 10:
                    var=x  
                    var.append(ele)
                    draft.append(var)
                    var=[]
                
                elif sum_t > 10:
                    continue
        
    f_ext_ele_groups=draft    
    length=len(draft)
mhhabib
  • 2,975
  • 1
  • 15
  • 29
  • Likely [python - List changes unexpectedly after assignment. How do I clone or copy it to prevent this? - Stack Overflow](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-how-do-i-clone-or-copy-it-to-prevent) . – user202729 Feb 07 '21 at 14:47

1 Answers1

0

FINALLY SOLVED. For use an element from a list in iteration, .copy() should be used ! I forgot it for var=x if you look carefully. It should be var=x.copy().