Given this 2D Array -
[[0, 0, -1], [0, 0, -2], [0, 0, 0], [0, 0, 3], [0, 0, 0]]
How can I code something in Python that shifts once to the end each time it's called? However, it has to stop if it reaches a positive value.
So I get something like this
[[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, 3], [0, 0, 0]]
This is what I have. It works for condition 1 but it does not work for condition 2. What can I tweak so condition 2 also works?
# Condition 1 - Works
data = [[0, 0, -1], [0, 0, -2], [0, 0, -3], [0, 0, 0], [0, 0, 0]]
expected = [[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, -3], [0, 0, 0]] # My Results.
# Condition 2 - Does Not Work
data = [[0, 0, -1], [0, 0, -2], [0, 0, 0], [0, 0, 3], [0, 0, 0]]
expected = [[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, 3], [0, 0, 0]] # This is what I want to get. (But it doesn't work)
rows = len(data)
if data[-1][-1] == 0:
count = rows-1
while count > 0:
data[count][-1] = data[count-1][-1]
count -= 1
data[0][-1] = 0
print(data)
print(expected)
This is what I am getting currently. I want to get the expected for Condition 2 listed in the code snipped above.:
Condition 2 Result:
[[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, 0], [0, 0, 3]]
Thanks
Update #Furas:
Something like this to find the positive value location?
possitive_value = []
for i in range(len(data)):
if data[i][-1] > 0:
possitive_value.append(i, -1)
I think you have to find first positive value and use its position as rows
EDIT: I changed name count to last to make it more readable
# --- function ---
def move(data):
rows = len(data)
# - find positive -
for x in range(rows):
if data[x][-1] > 0:
rows = x # use its position as `rows`
break # don't seach other positiove values
# - star moving -
# set "last" checked row
last = rows-1
# check "last" row
if data[last][-1] == 0:
# move previous values
while last > 0:
data[last][-1] = data[last-1][-1]
last -= 1
# put 0 in first place
data[0][-1] = 0
# --- tests ---
examples = [
{
# Condition 1 - Works
'data': [[0, 0, -1], [0, 0, -2], [0, 0, -3], [0, 0, 0], [0, 0, 0]],
'expected': [[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, -3], [0, 0, 0]], # My Results.
},
{
# Condition 2 - Works
'data': [[0, 0, -1], [0, 0, -2], [0, 0, 0], [0, 0, 3], [0, 0, 0]],
'expected': [[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, 3], [0, 0, 0]], # This is what I want to get. (But it doesn't work)
}
]
for example in examples:
data = example['data']
expected = example['expected']
print(' before:', data)
move(data)
print(' after:', data)
print('expected:', expected)
print(' correct:', data == expected)
print('---')
Result:
before: [[0, 0, -1], [0, 0, -2], [0, 0, -3], [0, 0, 0], [0, 0, 0]]
after: [[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, -3], [0, 0, 0]]
expected: [[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, -3], [0, 0, 0]]
correct: True
---
before: [[0, 0, -1], [0, 0, -2], [0, 0, 0], [0, 0, 3], [0, 0, 0]]
after: [[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, 3], [0, 0, 0]]
expected: [[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, 3], [0, 0, 0]]
correct: True
---
EDIT:
BTW: instead of while you can use for with reversed range() if it makes it more readable
def move(data):
rows = len(data)
# - find positive -
for x in range(rows):
if data[x][-1] > 0:
rows = x # use its position as `rows`
break # don't seach other positiove values
# - star moving -
# set "last" checked row
last = rows-1
# check "last" row
if data[last][-1] == 0:
# move previous values
for pos in range(last, 0, -1): # range with reversed order
data[pos][-1] = data[pos-1][-1]
# put 0 in first place
data[0][-1] = 0
BTW: both versions move items in original data ("in-place") so they don't need return data.
Here's a variation, a function that can be called and will shift data by one every time. I wasn't entirely clear from your question if this was the behavior you wanted.
import numpy as np
def shift_data(data):
ele_idx = [i for i,x in enumerate(data) if x != [0,0,0]]
zero_idx = [i for i,x in enumerate(data) if x == [0,0,0] and i != 0]
if max(np.diff(ele_idx)) == 1 or max(np.diff([i for i,x in enumerate(data) if x == [0,0,0]])) == 1:#things are consecutive
data.insert(0,data.pop(-1))
else:
tt = [l for l in data[ele_idx[0]:zero_idx[0]+1]]
tt.insert(0,tt.pop(-1))
data = data[:ele_idx[0]] + tt + data[zero_idx[0]+1:]
return data
data = [[0, 0, -1], [0, 0, 0], [0, 0, -2], [0, 0, 0], [0, 0, 3]]
print(data)
for _ in range(0,6):
data = shift_data(data)
print(data)
This code outputs:
[[0, 0, -1], [0, 0, 0], [0, 0, -2], [0, 0, 0], [0, 0, 3]]
[[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, 0], [0, 0, 3]]
[[0, 0, 0], [0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, 3]]
[[0, 0, 3], [0, 0, 0], [0, 0, 0], [0, 0, -1], [0, 0, -2]]
[[0, 0, -2], [0, 0, 3], [0, 0, 0], [0, 0, 0], [0, 0, -1]]
[[0, 0, -1], [0, 0, -2], [0, 0, 3], [0, 0, 0], [0, 0, 0]]
[[0, 0, 0], [0, 0, -1], [0, 0, -2], [0, 0, 3], [0, 0, 0]]
Related
Assuming the sudoku puzzle is a 9 by 9 and is filled with 0-9. I want is 9 lists, with each list containing a 3 by 3 Sudoku box.
This is what I have:
grid = [[5, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 8, 7, 0, 6, 0],
[0, 0, 0, 0, 0, 3, 0, 0, 0],
[0, 5, 0, 0, 6, 1, 0, 7, 0],
[0, 0, 2, 0, 0, 0, 9, 0, 0],
[0, 0, 0, 4, 0, 0, 0, 0, 0],
[0, 0, 0, 5, 0, 0, 0, 4, 0],
[9, 0, 0, 0, 4, 8, 7, 0, 0],
[0, 8, 0, 3, 0, 0, 0, 0, 0]]
list1 = []
for i in range(0,3):
for j in range(0,3):
list1.append(grid[i][j])
list2 = []
for i in range(0,3):
for j in range(4,7):
list2.append(grid[i][j])
and it goes on and on.......
How do I simplify this for loop for getting a list of 3 by 3 boxes in Sudoku puzzle?
You need to add a step parameter to your range()s so that you start reading in each box at the appropriate row / column index:
from itertools import product
boxes = []
for row_start, col_start in product(range(0, 9, 3), repeat=2):
boxes.append([
[grid[row][col] for col in range(col_start, col_start + 3)]
for row in range(row_start, row_start + 3)
])
print(boxes)
This outputs:
[
[[5, 0, 0], [0, 1, 0], [0, 0, 0]],
[[0, 0, 0], [0, 8, 7], [0, 0, 3]],
[[0, 0, 1], [0, 6, 0], [0, 0, 0]],
[[0, 5, 0], [0, 0, 2], [0, 0, 0]],
[[0, 6, 1], [0, 0, 0], [4, 0, 0]],
[[0, 7, 0], [9, 0, 0], [0, 0, 0]],
[[0, 0, 0], [9, 0, 0], [0, 8, 0]],
[[5, 0, 0], [0, 4, 8], [3, 0, 0]],
[[0, 4, 0], [7, 0, 0], [0, 0, 0]]
]
from pprint import pprint as pp
SIZE = 3
sudoku = [[[0] * SIZE for _ in range(SIZE)] for _ in range(SIZE * SIZE)]
# example of populating it
for i in range(SIZE):
for j in range(SIZE):
counter = i * SIZE + j
sudoku[counter][i][j] = counter + 1
pp(sudoku)
output
[[[1, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 2, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 3], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [4, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 5, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 6], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [7, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 8, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 9]]]
This question already has answers here:
How do I compute all possibilities for an array of numbers/bits (in python, or any language for that matter)
(5 answers)
Closed 2 years ago.
I am trying to achieve the following. I have a 2D array, which is of a 4x4 dimension. I want to get all possibilities, where I can insert a single 1 instead of a zero, and return an array, which contains all of these possibilities
So if we take:
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
would result in:
[[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
...
There would be a total of 14 entries in the resulting array, since there were 14 zeroes in the input array.
The problem is, that the code I have currently should work, as far as I understand, but I can't seem to get where it goes wrong.
def getPossibilities(arr):
p = []
for i in range(4):
for j in range(4):
if arr[i][j] == 0:
p.append(arr)
p[-1][i][j]=1
return p
for i in getPossibilities([[0,1,0,0],[0,0,1,0],[0,0,0,0],[0,0,0,0]]):
print(i)
This results in 14 arrays of solid ones.
I included the way I check the results, in case there is an error there. I also tried with first copying the arr array into a temporary one, then make the changes, but to no avail.
What goes wrong here? I cannot seem to find an answer. Also, is there a more elegant and faster way of doing this? It would be really beneficial for my usecase.
Thank you very much in advance!
This is somewhat tricky but since you have a list of lists, the copy won't work and you will be changing the array every time, what you need is deepcopy:
import copy
def getPossibilities(arr):
p = []
for i in range(4):
for j in range(4):
if arr[i][j] == 0:
tmp = copy.deepcopy(arr)
tmp[i][j]=1
p.append(tmp)
return p
for i in getPossibilities([[0,1,0,0],[0,0,1,0],[0,0,0,0],[0,0,0,0]]):
print(i)
[[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [1, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [1, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 1, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 1, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 1]]
I would like to initialize a multidimensional list capable of storing an object for every minute since year 1950-2050.
Something like:
minute = [None]*61
hour = [minute]*25
day = [hour]
month = [day]
data = [month]*100
So you can do:
data[89][1][29][23][55] = 'It was a good minute the one from January 29th in 1989 when it was 23:55'
How would be such a multidimensional list be initialized in Python? Would it be an actual different object than the one created with the above code?
Initially the multidimensional list would contain objects None.
Python 2.7
Following answer I tried:
# Data structure
minute = 60
hour = 24
day = 31
month = 12
year = 100
test = [[[[[None for _minute in range(minute)] for _hour in range(hour)] for _day in range(day)] for _month in range(month)] for _year in range(year)]
But it seems too much for multidimensional lists, as I get "Killed" when trying to execute this.
I also don't recommend this, but you could use a numpy.chararray for this:
import numpy as np
arr = np.chararray((100, 12, 31, 24, 60, 60), itemsize=100)
arr[52, 7, 12, 12, 44, 54] = 'year 1950+52, 7th month, 12th day, 12th hour, 44th minute, 54th second'
I'm not exactly sure what your desired structure is, but the string I inserted into the array should explain the structure I proposed, and you can change it however you need. Note that itemsize limits how many characters you can put in at any index.
Again, with a caveat that this is not necessarily the most efficient thing in the world to do, but if you wish to store lists of ints and/or floats in that array (as per your comment), one way to do it would be to convert that list to strings, and then when retrieving it, re-transform back to a list:
data_to_insert = [1,2,3,4.5]
# store as string
arr[52, 7, 12, 12, 44, 54] = ','.join(map(str, data_to_insert))
# retrieve
arr[52, 7, 12, 12, 44, 54].decode('utf-8').split(',')
This should be pretty fast
Though i won't recommend it,
A multi dimentional empty list can be created by using list comprehension:
> >>> a = 4 #Width of elements
> >>> b = 6 #Width of main list container
>>>>> c = 4
>>>>> d = 3
> >>> l = [[[[0 for k in range(d) ] for z in range(c)] for x in range(a)] for y in range(b)]
> >>> [[[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]], [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]], [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]], [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]], [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]], [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]]
Keep replacing 0 with list comprehensions to add more dimentions.
Given this 2D Array - [[0, 0, -1], [1, 0, -2], [2, 0, -3], [3, 0, 0]]
How can I code something in Python that moves everything to the end? (If the end is empty).
So I get something like this - [[0, 0, 0], [1, 0, -1], [2, 0, -2], [3, 0, -3]]
I tried something like this:
count = 0
while count < row: # row is 4 in this case.
if my_array[row-1][2] == 0:
tp = my_array[count][2]
my_array[count][2] = 0
my_array[count+1][2] = tp
else:
break
count += 1
return my_array
But this is what I get instead:
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, -1]]
Update:
Thanks #furas, I spent hours trying to figure this out
How can I also handle a gap in the middle? So if I get something like this:
data = [[0, 0, -1], [0, 0, 0], [0, 0, -3], [0, 0, -4]]
expected = [[0, 0, 0], [0, 0, -1], [0, 0, -3], [0, 0, -4]]
rows = len(data)
if data[-1][-1] == 0:
count = rows-1
while count > 0:
data[count][-1] = data[count-1][-1]
count -= 1
data[0][-1] = 0
print(data)
print(expected)
print(data == expected) # False
Thanks
UPDATE Nov.29 -
I have certain scenarios like these that don't work? Can you help?
DATA: [[0, 0, 0, 0], [1, 2, 1, 0], [2, 1, 2, 0], [0, 0, 0, 0]]
EXPECT: [[0, 0, 0, 0], [0, 0, 0, 0], [1, 2, 1, 0], [2, 1, 2, 0]]
Or if it's negative:
DATA: [[0, 0, 0, 0], [-101, -101, -101, 0], [2, 3, 2, 0], [3, 2, 3, 0]]
EXPECT: [[0, 0, 0, 0], [0, 0, 0, 0], [2, 3, 2, 0], [3, 2, 3, 0]]
Thanks A lot!!
I got something like this
def move(data):
row = len(data)-1
prev = row-1
while row > 0 and prev >= 0:
if data[row][-1] == 0:
while prev >= 0 and data[prev][-1] == 0:
prev -= 1
data[row][-1] = data[prev][-1]
data[prev][-1] = 0
row -= 1
prev -= 1
return data
# --- test ---
examples = [
{
'data': [[0, 0, -1], [0, 0, 0], [0, 0, -3], [0, 0, -4]],
'expected': [[0, 0, 0], [0, 0, -1], [0, 0, -3], [0, 0, -4]],
},
{
'data': [[0, 0, -1], [1, 0, -2], [2, 0, -3], [3, 0, 0]],
'expected': [[0, 0, 0], [1, 0, -1], [2, 0, -2], [3, 0, -3]],
},
]
for ex in examples:
print(ex['data'])
result = move(ex['data'])
print(ex['expected'])
print(result == ex['expected']) # True
I use rows = len(data) and [-1] so it can have any size.
External while check if place row is empty and start internal while which is looking for not-empty elements prev before row. After that it move non-empty element from prev to row and put zero in prev. And it starts again with next row place.
It looks more complicated then other solutions.
Here's a reasonably efficient way to do this. We grab the last column of the grid into a list named lastcol, and then sort lastcol with a key function that tests if each item is not equal to zero. That will move all the zeros to the start of lastcol without disturbing the order of the other elements. And then we copy lastcol back to the grid.
I've modified your test data slightly so that we can see that the code handles gaps and positive & negative values correctly.
grid = [[0, 0, -1], [1, 0, 2], [2, 0, 0], [3, 0, -3], [4, 0, 0]]
print(grid)
lastcol = [u[-1] for u in grid]
lastcol.sort(key=(0).__ne__)
for row, u in zip(grid, lastcol):
row[-1] = u
print(grid)
output
[[0, 0, -1], [1, 0, 2], [2, 0, 0], [3, 0, -3], [4, 0, 0]]
[[0, 0, 0], [1, 0, 0], [2, 0, -1], [3, 0, 2], [4, 0, -3]]
Assuming I'm understanding the goal correctly -- that you'd want a column [0, 1, 0, 2, 3] to become [0, 0, 1, 2, 3] -- then with the use of a helper function to transpose the array, all we need to do is sort the columns using bool as the key function, because non-zero numbers are false, and False < True.
def transpose(seq):
return [list(part) for part in zip(*seq)]
def push(seq):
return transpose(sorted(part, key=bool) for part in transpose(seq))
which gives me
In [29]: push([[0, 0, -1], [1, 0, -2], [2, 0, -3], [3, 0, 0]])
Out[29]: [[0, 0, 0], [1, 0, -1], [2, 0, -2], [3, 0, -3]]
In [30]: push([[0, 0, -1], [0, 0, 0], [0, 0, -3], [0, 0, -4]])
Out[30]: [[0, 0, 0], [0, 0, -1], [0, 0, -3], [0, 0, -4]]
#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.
Append a copy of the list instead of the list itself.
deck.append(filler[:])
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]]
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)
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)