Say, I have a 2-dimensional list/array that contains 50 subarrays that look like [0, 0]. I want to change only some parts of the array, say the first elements of each subarray from index 15 to 29. I used a for loop to do this.
array = [[0,0]] * 50
for i in range(15, 30):
array[i][0] = 1
But when I print(array), it seems like the program changes all the first elements of each subarray. Output:
[[1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0]]
I do not know what I am doing wrong, as the logic seems sound. Can anyone identify my mistake?
array = [[0,0]] * 50 By this, your array will have [0,0] 50 times referencing to same [0,0]. So modifying one will modify all of them.
Instead try this: array = [[0, 0] for _ in range(50)].
Related
for example I have this list:
lst = [[0, 1], [0, 1], [0, 1], [1, 0], [1, 0], [1, 0]]
and I shuffle it for example with seed = 42:
random.seed(42)
random.shuffle(lst)
I took this list after shuffle:
[[1, 0], [0, 1], [0, 1], [1, 0], [0, 1], [1, 0]]
I want to take oposite of this shuffle too
How can I take this list:
[[0, 1], [1, 0], [1, 0], [0, 1], [1, 0], [0, 1]]
I want oposite seed
I am trying to manipulate array2 so that the row and column is dependent on the len of the strings as for array1
str1 = "Hi"
str2 = "Bye"
array1 = [[[0, 0] for y in range(len(str2)+1)] for x in range(len(str1)+1)]
print(array1)
#output: [[[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]]]
array2 = [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
#want array2 to implement same format as array1 where the row and column is determined by the len of str1 and str2
temp = [[[array2[i], array2[j]] for y in range(len(str2)+1)] for x in range(len(str1)+1)] #does not work
I tried to remove some brackets from temp however, did not work.
I tried to manipulate the method I used for array1, but did not work. I was expecting the rows and columns to be dependent on the len of the strings as for array2.
The current code has no idea what to do with array2[i], array2[j] cause neither i nor j have been defined.
This code is working as expected (I've organized the output for better readability):
str1 = "Hi"
str2 = "Bye"
array1 = [[[0, 0] for y in range(len(str2)+1)] for x in range(len(str1)+1)]
print(array1)
#output is 4x3: [
#[[0, 0], [0, 0], [0, 0], [0, 0]],
#[[0, 0], [0, 0], [0, 0], [0, 0]],
#[[0, 0], [0, 0], [0, 0], [0, 0]]]
array2 = [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
temp = [[[0, 0] for y in range(len(str2)+1)] for x in range(len(str1)+1)]
#output is 4x3: [
#[[0, 0], [0, 0], [0, 0], [0, 0]],
#[[0, 0], [0, 0], [0, 0], [0, 0]],
#[[0, 0], [0, 0], [0, 0], [0, 0]]]
If you want to use a certain set of numbers from the array, you need to change the code to:
i, o = 0, 1
temp = [[[array2[i], array2[j]] for y in range(len(str2)+1)] for x in range(len(str1)+1)]
If this doesn't solve your problem, please provide further explanation
Say I have the following two arrays, a and b:
import numpy as np
a = np.array([[[1, 0],
[1, 1]],
[[1, 0],
[0, 0]],
[[0, 0],
[1, 0]]])
b = np.array([[[0, 2],
[0, 0]],
[[0, 0],
[0, 2]],
[[0, 2],
[0, 2]]])
and I wish to 'overlap' them so that I get the following result:
[[[1, 2],
[1, 1]],
[[1, 0],
[0, 2]],
[[0, 2],
[1, 2]]]
In the case there is an overlapping co-ordinate, I would just take 1. How could I achieve this?
Maybe you can start with a matrix with zeros and then assign the flags one by one:
import numpy as np
a = np.array([[[1, 0],
[1, 1]],
[[1, 0],
[0, 0]],
[[0, 0],
[1, 0]]])
b = np.array([[[0, 2],
[0, 0]],
[[0, 0],
[0, 2]],
[[0, 2],
[0, 2]]])
# Create a matrix with zeros
c = np.zeros(a.shape, dtype='int')
# Assign flags
c[b==2] = 2
c[a==1] = 1 # Put in second because priority to 1 in case of overlapping
# Output
print(c)
Output:
array([[[1, 2],
[1, 1]],
[[1, 0],
[0, 2]],
[[0, 2],
[1, 2]]])
Using a simple sum I manage to get the desired result:
import numpy as np
a = np.array([[[1, 0],
[1, 1]],
[[1, 0],
[0, 0]],
[[0, 0],
[1, 0]]])
b = np.array([[[0, 2],
[0, 0]],
[[0, 0],
[0, 2]],
[[0, 2],
[0, 2]]])
print(a+b)
Output:
[[[1 2]
[1 1]]
[[1 0]
[0 2]]
[[0 2]
[1 2]]]
As an attempt to further my knowledge in python, I have started to create a very simple tic tac toe AI.
Currently, I am stumped at some behavior I have not expected from python where when I append a class instance variable to a local list and change the item in the local list, the instance variable will have changed too.
How can I change only the local list element without affecting the class instance variable?
This is the extract of the program which is affected:
class ticAI:
def __init__(self, board):
self.board = board
self.tic = tictactoe(board)
def calc(self):
possibilities = []
ycord = 0
for y in self.board:
xcord = 0
for x in y:
if x == 0:
possibilities.append(self.board)
possibilities[len(possibilities)-1][ycord][xcord] = 2
print(self.board)
xcord += 1
ycord += 1
self.board looks like this:
[
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
]
and outputs this:
[[2, 0, 0], [0, 1, 0], [0, 0, 0]]
[[2, 2, 0], [0, 1, 0], [0, 0, 0]]
[[2, 2, 2], [0, 1, 0], [0, 0, 0]]
[[2, 2, 2], [2, 1, 0], [0, 0, 0]]
[[2, 2, 2], [2, 1, 2], [0, 0, 0]]
[[2, 2, 2], [2, 1, 2], [2, 0, 0]]
[[2, 2, 2], [2, 1, 2], [2, 2, 0]]
[[2, 2, 2], [2, 1, 2], [2, 2, 2]]
it should however, output this:
[[2, 0, 0], [0, 1, 0], [0, 0, 0]]
[[0, 2, 0], [0, 1, 0], [0, 0, 0]]
[[0, 0, 2], [0, 1, 0], [0, 0, 0]]
[[0, 0, 0], [2, 1, 0], [0, 0, 0]]
[[0, 0, 0], [0, 1, 2], [0, 0, 0]]
[[0, 0, 0], [0, 1, 0], [2, 0, 0]]
[[0, 0, 0], [0, 1, 0], [0, 2, 0]]
[[0, 0, 0], [0, 1, 0], [0, 0, 2]]
As made aware by #jonrsharpe, you can use deepcopy to create a copy of a variable.
Original code:
possibilities.append(self.board)
possibilities[len(possibilities)-1][ycord][xcord] = 2
print(self.board)
New code:
b = copy.deepcopy(self.board)
possibilities.append(b)
possibilities[len(possibilities)-1][ycord][xcord] = 2
print(self.board)
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 6 years ago.
c_k_list = [[0, 0]]*(sorted_degrees[len(sorted_degrees)-1]+1)
c_k_list[entry[1]][0] = c_k_list[entry[1]][0]+1
where entry[1]=1
In the above statement, instead of adding 1 to a particular element in c_k_list, it adds 1 to all the rows.
Eg:
c_k_list is
[[1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0]]
instead of
[[0,0], [1,0], [0,0]......[0,0]]
Lists are objects, and so are stored by reference. Using * will just create copies of that reference. To correct this try:
c_k_list = [[0, 0] for i in range(5)]
c_k_list[1][0] = c_k_list[1][0]+1