Roman Formicola
Roman Formicola

Reputation: 95

How do I re-write a variable, yet save the previous information in a List

import random

Deck = [1,2,3,4,5,6]
Attempts = [] 
times = 0

while times <= 5:

    number1 = random.randrange(0,3)
    number2 = random.randrange(0,3)
    if number2 == number1:
        number2 = random.randrange(0,3)

    Deck[number1], Deck[number2] = Deck[number2], Deck[number1]

    print Deck
    newDeck = Deck

    Attempts.append(newDeck)

    print Attempts

    times += 1

if times == 6:
    print Attempts

So essentially this randomly switches two numbers, and then prints out the new combination. I want to be able to save every list of numbers in Attempts. I want to do this without creating five variables. when I run this everything works except all of Attempts number lists turn into the last combination to be randomized.

Upvotes: 0

Views: 70

Answers (3)

koffein
koffein

Reputation: 1882

This answer contains the same list-copy hint like @Amber posted, but I added something I consider useful advice for programming in Python.

import random

# Variables are normally written in lowercase.
# It is a convention, but syntax highlighting makes use of it, for example.
deck = [1,2,3,4,5,6]
attempts = [] 

# Every time you know, how many times something is happening, you should
# use a for-loop: You get rid of three lines, that spread over the script...
for times in range(5):
    # The sample-function of the random module does exactly what you need:
    # two different numbers from the same range...
    number1, number2 = random.sample(range(3), 2)
    deck[number1], deck[number2] = deck[number2], deck[number1]

    # There was no particular reason to change the output,
    # but maybe it shows how string formating is handled in Python.
    print "Attempt {}:".format(times), deck

    # appends a copy of the current deck to the attempts-list
    # tuple(deck) would be another way to go.
    attempts.append(list(deck))


print "All attempts", attempts

Output:

Attempt 0: [1, 3, 2, 4, 5, 6]
Attempt 1: [2, 3, 1, 4, 5, 6]
Attempt 2: [2, 1, 3, 4, 5, 6]
Attempt 3: [3, 1, 2, 4, 5, 6]
Attempt 4: [3, 2, 1, 4, 5, 6]
All attempts [[1, 3, 2, 4, 5, 6], [2, 3, 1, 4, 5, 6], [2, 1, 3, 4, 5, 6], [3, 1, 2, 4, 5, 6], [3, 2, 1, 4, 5, 6]]

I noticed that you are swapping the first three cards only. Is this behaviour intended?

Here are some further sources regarding the sample-function, for-loops and Python string formatting.

Upvotes: 0

vHalaharvi
vHalaharvi

Reputation: 179

You are basically appending the reference to the array. You are not truly cloning it. Try this instead of Attempts.append(newDeck).

from copy import deepcopy
Attempts.append(deepcopy(newDeck))

Upvotes: 0

Amber
Amber

Reputation: 526613

You can create a copy of the list that you append, instead of appending the same list repeatedly:

newDeck = list(Deck)

The reason you're running into problems is that newDeck = Deck doesn't actually create a copy.

Upvotes: 2

Related Questions