Ryan Marvin
Ryan Marvin

Reputation: 47

Python List Assignment Issues

#DeckOfCards
deck = []

filler= [0, 0, 0, 0]

def deck_generator():

    counter = 0
    for i in range (52):
        counter += 1
        deck.append(filler)
    return deck

def deck_values(i):

    k = 4
    temp = (i + 1) % k
    return temp
deck = deck_generator()

for i in range(52):

    deck[i][0] = deck_values(i)

The goal with this code is to assign the values 0-3 inclusive to the first index of the inner list to all values in the outer list. [[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], [0, 0, 0, 0]] and so on. For some reason the assignment just does not work. Thanks in advance.

Upvotes: 0

Views: 170

Answers (4)

Daniyal Ahmed
Daniyal Ahmed

Reputation: 755

I think the issue with it is temp=(i+1)%k as we do not need to add 1 to 1. It should start from 0. In addition to this, you need to append properly so it works. The code would look like this:

#DeckOfCards
deck = []

filler= [0, 0, 0, 0]

def deck_generator():

    counter = 0
    for i in range (52):
        counter += 1
        deck.append(filler[:])
    return deck

def deck_values(i):

    k = 4
    temp = (i) % k
    return temp

deck = deck_generator()

for i in range(52):

    deck[i][0] = deck_values(i)

Upvotes: 0

Hejun
Hejun

Reputation: 376

are you sure you want to get [[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], [0, 0, 0, 0]] ?

first of all, you should use the copy of filter and then you can get a list like:
[[1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], [0, 0, 0, 0],...]

but if you want to get the result [[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],...] your codes should be like this:
deck = []

filler= [0, 0, 0, 0]

def deck_generator():

    counter = 0
    for i in range (52):
        counter += 1
        deck.append(filler[:])
    return deck

def deck_values(i):

    k = 4
    temp = i % k      #not temp = (i+1) % k
    return temp


deck = deck_generator()

for i in range(52):

    deck[i][0] = deck_values(i)

print(deck)

Upvotes: 0

Sci Prog
Sci Prog

Reputation: 2691

Try this

deck = []
for i in range(52):
  deck.append([i % 4, 0, 0, 0])
print (deck)

Running this code prints (edited for ease of viewing):

[[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], 
 [0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0]]

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

Append a copy of the list instead of the list itself.

deck.append(filler[:])

Upvotes: 1

Related Questions