Apr-09-2020, 03:10 AM
So I am trying to create a list (called megalist) of all of the 3 digit combinations from the list of numbers called numbers. I think the logic is sound (I am testing the process with print commands and they seem to suggest all is well.) But for some reason the append to is not simply appending. Each time a new 3-digit combo is created and added to megalist it replaces the previous combos collected with the newest combo as well. I can't figure out why!
So,
megalist begins empty.
First combo is created [1,3,5]
megalist becomes [[1,3,5]]
next combo is created [1,3,7]
So,
megalist begins empty.
First combo is created [1,3,5]
megalist becomes [[1,3,5]]
next combo is created [1,3,7]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
numbers = [ 1 , 3 , 5 , 7 , 9 , 11 , 13 , 15 ] megalist = [] list_of_3 = [ - 1 , - 1 , - 1 ] for first in range ( len (numbers) - 1 ): #selecting first number from the 1 till 11 first_num = numbers[first] list_of_3[ 0 ] = first_num print ( 'list of 3 is ' , list_of_3) for second in range ( 1 + numbers.index(list_of_3[ 0 ]), len (numbers)): #selecting second 3 to 13 second_num = numbers[second] list_of_3[ 1 ] = second_num print ( 'list of 3 is ' , list_of_3) for third in range ( 1 + numbers.index(list_of_3[ 1 ]), len (numbers) + 1 ): #selecting 3rd 5 to 15 third_num = numbers[third] print ( '3rd number is ' , third_num) list_of_3[ 2 ] = third_num print ( 'list of 3 is ' , list_of_3) megalist.insert( 0 ,list_of_3) print ( 'combos = ' ,megalist) |