result appending to list only as last result












0















I'm trying to make a short program to determine every combination of four dice that multiplies to make a product of 72 and it works fine if I print out the combinations that make 72 within the for loop but if I try to append it to a results list it only returns [6,6,6,6] (the last possible combination).



# code to determine every combination of 4 dice that multiplys to 72
# nxt function creates next possible combination eg [0, 1, 4, 6] becomes [0, 1, 5, 0]
def nxt(number):

carry = 0
for i in range(len(number) - 1, -1, -1):
if i == len(number) - 1:
number[i] += 1
if number[i] > 6:
carry = 1
number[i] = 0
else:
number[i] += carry
if number[i] > 6:
carry = 1
number[i] = 0
else:
carry = 0
return number


dice = [0, 0, 0, 0]
result =

#2400 is number of combinations possible (7^4)
for i in range(0, 2400):
if dice[0] * dice[1] * dice[2] * dice[3] == 72:
result.append(dice)
dice = nxt(dice)

print(result)


returns an equal number of [6,6,6,6] lists as there are combinations that make 72 where as if print the result one at a time instead of appending



dice = [0, 0, 0, 0]
result =

for i in range(0, 2400):
if dice[0] * dice[1] * dice[2] * dice[3] == 72:
print dice
dice = nxt(dice)


it prints them out just fine










share|improve this question


















  • 1





    Do your dice have seven sides (implied by their values being in the range 0..6)? It would also mean that each has one face that's blank.

    – martineau
    Nov 22 '18 at 21:20











  • no they are six sided dice. I realised this issue but seing as x * 0 = 0 I never bothered to change it

    – Numdoo
    Nov 22 '18 at 21:23
















0















I'm trying to make a short program to determine every combination of four dice that multiplies to make a product of 72 and it works fine if I print out the combinations that make 72 within the for loop but if I try to append it to a results list it only returns [6,6,6,6] (the last possible combination).



# code to determine every combination of 4 dice that multiplys to 72
# nxt function creates next possible combination eg [0, 1, 4, 6] becomes [0, 1, 5, 0]
def nxt(number):

carry = 0
for i in range(len(number) - 1, -1, -1):
if i == len(number) - 1:
number[i] += 1
if number[i] > 6:
carry = 1
number[i] = 0
else:
number[i] += carry
if number[i] > 6:
carry = 1
number[i] = 0
else:
carry = 0
return number


dice = [0, 0, 0, 0]
result =

#2400 is number of combinations possible (7^4)
for i in range(0, 2400):
if dice[0] * dice[1] * dice[2] * dice[3] == 72:
result.append(dice)
dice = nxt(dice)

print(result)


returns an equal number of [6,6,6,6] lists as there are combinations that make 72 where as if print the result one at a time instead of appending



dice = [0, 0, 0, 0]
result =

for i in range(0, 2400):
if dice[0] * dice[1] * dice[2] * dice[3] == 72:
print dice
dice = nxt(dice)


it prints them out just fine










share|improve this question


















  • 1





    Do your dice have seven sides (implied by their values being in the range 0..6)? It would also mean that each has one face that's blank.

    – martineau
    Nov 22 '18 at 21:20











  • no they are six sided dice. I realised this issue but seing as x * 0 = 0 I never bothered to change it

    – Numdoo
    Nov 22 '18 at 21:23














0












0








0








I'm trying to make a short program to determine every combination of four dice that multiplies to make a product of 72 and it works fine if I print out the combinations that make 72 within the for loop but if I try to append it to a results list it only returns [6,6,6,6] (the last possible combination).



# code to determine every combination of 4 dice that multiplys to 72
# nxt function creates next possible combination eg [0, 1, 4, 6] becomes [0, 1, 5, 0]
def nxt(number):

carry = 0
for i in range(len(number) - 1, -1, -1):
if i == len(number) - 1:
number[i] += 1
if number[i] > 6:
carry = 1
number[i] = 0
else:
number[i] += carry
if number[i] > 6:
carry = 1
number[i] = 0
else:
carry = 0
return number


dice = [0, 0, 0, 0]
result =

#2400 is number of combinations possible (7^4)
for i in range(0, 2400):
if dice[0] * dice[1] * dice[2] * dice[3] == 72:
result.append(dice)
dice = nxt(dice)

print(result)


returns an equal number of [6,6,6,6] lists as there are combinations that make 72 where as if print the result one at a time instead of appending



dice = [0, 0, 0, 0]
result =

for i in range(0, 2400):
if dice[0] * dice[1] * dice[2] * dice[3] == 72:
print dice
dice = nxt(dice)


it prints them out just fine










share|improve this question














I'm trying to make a short program to determine every combination of four dice that multiplies to make a product of 72 and it works fine if I print out the combinations that make 72 within the for loop but if I try to append it to a results list it only returns [6,6,6,6] (the last possible combination).



# code to determine every combination of 4 dice that multiplys to 72
# nxt function creates next possible combination eg [0, 1, 4, 6] becomes [0, 1, 5, 0]
def nxt(number):

carry = 0
for i in range(len(number) - 1, -1, -1):
if i == len(number) - 1:
number[i] += 1
if number[i] > 6:
carry = 1
number[i] = 0
else:
number[i] += carry
if number[i] > 6:
carry = 1
number[i] = 0
else:
carry = 0
return number


dice = [0, 0, 0, 0]
result =

#2400 is number of combinations possible (7^4)
for i in range(0, 2400):
if dice[0] * dice[1] * dice[2] * dice[3] == 72:
result.append(dice)
dice = nxt(dice)

print(result)


returns an equal number of [6,6,6,6] lists as there are combinations that make 72 where as if print the result one at a time instead of appending



dice = [0, 0, 0, 0]
result =

for i in range(0, 2400):
if dice[0] * dice[1] * dice[2] * dice[3] == 72:
print dice
dice = nxt(dice)


it prints them out just fine







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 21:00









NumdooNumdoo

31




31








  • 1





    Do your dice have seven sides (implied by their values being in the range 0..6)? It would also mean that each has one face that's blank.

    – martineau
    Nov 22 '18 at 21:20











  • no they are six sided dice. I realised this issue but seing as x * 0 = 0 I never bothered to change it

    – Numdoo
    Nov 22 '18 at 21:23














  • 1





    Do your dice have seven sides (implied by their values being in the range 0..6)? It would also mean that each has one face that's blank.

    – martineau
    Nov 22 '18 at 21:20











  • no they are six sided dice. I realised this issue but seing as x * 0 = 0 I never bothered to change it

    – Numdoo
    Nov 22 '18 at 21:23








1




1





Do your dice have seven sides (implied by their values being in the range 0..6)? It would also mean that each has one face that's blank.

– martineau
Nov 22 '18 at 21:20





Do your dice have seven sides (implied by their values being in the range 0..6)? It would also mean that each has one face that's blank.

– martineau
Nov 22 '18 at 21:20













no they are six sided dice. I realised this issue but seing as x * 0 = 0 I never bothered to change it

– Numdoo
Nov 22 '18 at 21:23





no they are six sided dice. I realised this issue but seing as x * 0 = 0 I never bothered to change it

– Numdoo
Nov 22 '18 at 21:23












1 Answer
1






active

oldest

votes


















1














The issue is in the for loop. In the append to be exact. It should be:



result.append(dice[:])


What you did just copies the reference several times. Meaning all the lists inside the big list take the same value because they are essentially the same list. What I did is copy the actual value to the list, not the reference.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437925%2fresult-appending-to-list-only-as-last-result%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The issue is in the for loop. In the append to be exact. It should be:



    result.append(dice[:])


    What you did just copies the reference several times. Meaning all the lists inside the big list take the same value because they are essentially the same list. What I did is copy the actual value to the list, not the reference.






    share|improve this answer






























      1














      The issue is in the for loop. In the append to be exact. It should be:



      result.append(dice[:])


      What you did just copies the reference several times. Meaning all the lists inside the big list take the same value because they are essentially the same list. What I did is copy the actual value to the list, not the reference.






      share|improve this answer




























        1












        1








        1







        The issue is in the for loop. In the append to be exact. It should be:



        result.append(dice[:])


        What you did just copies the reference several times. Meaning all the lists inside the big list take the same value because they are essentially the same list. What I did is copy the actual value to the list, not the reference.






        share|improve this answer















        The issue is in the for loop. In the append to be exact. It should be:



        result.append(dice[:])


        What you did just copies the reference several times. Meaning all the lists inside the big list take the same value because they are essentially the same list. What I did is copy the actual value to the list, not the reference.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 21:14

























        answered Nov 22 '18 at 21:08









        Esteban QuirosEsteban Quiros

        1015




        1015
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437925%2fresult-appending-to-list-only-as-last-result%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Paul Cézanne

            UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

            Angular material date-picker (MatDatepicker) auto completes the date on focus out