Python matrix: instead of updating an element it updates all rows [duplicate] - python

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

Related

I want oposite seed in my list how can I solve it

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

Filling a Pandas table with values

First of all sorry for the kind-of misleading title but I didn't know how to word that properly. My problem is that basically I don't know how I can fill a Pandas table with the values below. I need a table with rows and columns going from 0 to X (in which X is the maximum value in the "[X,Y]" brackets below), which i managed to make with Numpy, but I don't know how to insert the corresponding value_counts() data.
import pandas as pd
det_vect1 = [[0, 1], [1, 1], [0, 1], [0, 1], [1, 0], [0, 0], [0, 0], [0, 0], [1, 1], [0, 1], [0, 0], [0, 0], [0, 0], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 0], [0, 0], [0, 0], [0, 0], [1, 0], [0, 0], [0, 1], [0, 0], [3, 1], [0, 0], [0, 0], [0, 0], [2, 0], [1, 0], [3, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [1, 0], [0, 0], [0, 1], [0, 0], [1, 0], [0, 0], [2, 1], [0, 0], [0, 0], [1, 0], [0, 0], [0, 0], [0, 1], [1, 0], [0, 0], [0, 0], [0, 0], [2, 0], [0, 0], [0, 1], [0, 0], [0, 0], [6, 0], [0, 1], [0, 1], [2, 0], [0, 0], [0, 1], [0, 0], [0, 0], [0, 1], [1, 0], [2, 1], [0, 0], [0, 1], [0, 0], [0, 1], [1, 0], [0, 1], [0, 1], [0, 0], [0, 1], [0, 1], [1, 0], [0, 0], [1, 0], [0, 1], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [0, 0], [0, 0], [1, 0], [2, 0], [0, 0], [1, 0], [0, 1], [0, 0], [2, 1], [0, 1], [2, 0], [1, 0], [0, 1], [0, 0], [0, 0], [0, 1], [0, 0], [1, 0], [2, 0], [0, 2], [1, 0], [0, 1], [0, 1], [0, 0], [1, 0], [0, 0], [0, 0], [0, 1], [0, 0], [0, 1], [0, 1], [0, 0], [0, 0], [0, 1], [0, 0], [0, 1], [0, 1], [0, 0], [0, 1], [1, 0], [0, 1], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [0, 0], [0, 0], [0, 1], [0, 1], [0, 0], [0, 0], [0, 1], [0, 0], [0, 1], [0, 0], [0, 0], [0, 1], [0, 1], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [0, 1], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [1, 0], [0, 0], [1, 0], [0, 1], [2, 1], [0, 1], [0, 0], [0, 0], [0, 0], [0, 0], [1, 0], [0, 0], [0, 1], [1, 0], [0, 3], [0, 0], [0, 0], [1, 1], [0, 1], [0, 0], [0, 0], [1, 0], [0, 0], [1, 0], [0, 0], [0, 1], [1, 2], [0, 1], [1, 0], [1, 0], [1, 1], [0, 1], [1, 0], [0, 0], [0, 1], [0, 0], [0, 3], [0, 1], [0, 0], [0, 0], [0, 0], [0, 0], [2, 1], [0, 1], [1, 1], [0, 0], [0, 0], [0, 1], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [0, 1], [0, 0]]
print(pd.Series(det_vect1).value_counts())
which gives
[0, 0] 116
[0, 1] 64
[1, 0] 27
[2, 0] 6
[1, 1] 5
[2, 1] 5
[0, 3] 2
[3, 1] 1
[3, 0] 1
[6, 0] 1
[0, 2] 1
[1, 2] 1
dtype: int64
So i need something like (sorry for the scrappy Paint example with random values):
Of course the non-existing values can be easily filled with zeros, no problem.
Thank you in advance!
Use a DataFrame constructor, not Series, then unstack:
out = pd.DataFrame(det_vect1).value_counts().unstack(fill_value=0)
Or, with crosstab:
df_tmp = pd.DataFrame(det_vect1)
df = pd.crosstab(df_tmp[0], df_tmp[1])
output:
1 0 1 2 3
0
0 116 64 1 2
1 27 5 1 0
2 6 5 0 0
3 1 1 0 0
6 1 0 0 0
For a complete index of values, reindex:
(pd.DataFrame(det_vect1)
.value_counts().unstack(fill_value=0) # or crosstab alternative
.pipe(lambda d: d.reindex(index=range(d.index.max()+1),
columns=range(d.columns.max()+1),
fill_value=0
)
)
.rename_axis(index=None, columns=None)
)
output:
0 1 2 3
0 116 64 1 2
1 27 5 1 0
2 6 5 0 0
3 1 1 0 0
4 0 0 0 0
5 0 0 0 0
6 1 0 0 0

Python 2D Array/List Assignment Gives Unwanted Result

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)].

Python class variable referenced from within list

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)

Sort a list of lists containing integers

I've got a list of lists containing integers sorted at the moment by the sum of the contents:
[[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0], [0, 0, 3], [0, 1, 2], [0, 2, 1], [0, 3, 0], [1, 0, 2], [1, 1, 1], [1, 2, 0], [2, 0, 1], [2, 1, 0], [3, 0, 0], [0, 0, 4], [0, 1, 3], [0, 2, 2], [0, 3, 1], [0, 4, 0], [1, 0, 3], [1, 1, 2], [1, 2, 1], [1, 3, 0], [2, 0, 2], [2, 1, 1], [2, 2, 0], [3, 0, 1], [3, 1, 0], [4, 0, 0]]
I would like to sort them in ascending order by the common structure of its contents i.e like
[[1, 0, 0], [2, 0, 0], [3, 0, 0], [4, 0, 0], [0, 1, 0], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 0, 1], [0, 0, 2], [0,0,3], [0,0,4]... ]
I have seen the docs but I can't figure out how I can do this.
Is this what you're after...
>>> l = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0], [0, 0, 3], [0, 1, 2], [0, 2, 1], [0, 3, 0], [1, 0, 2], [1, 1, 1], [1, 2, 0], [2, 0, 1], [2, 1, 0], [3, 0, 0], [0, 0, 4], [0, 1, 3], [0, 2, 2], [0, 3, 1], [0, 4, 0], [1, 0, 3], [1, 1, 2], [1, 2, 1], [1, 3, 0], [2, 0, 2], [2, 1, 1], [2, 2, 0], [3, 0, 1], [3, 1, 0], [4, 0, 0]]
>>> l.sort(key=lambda x: (-x.count(0), x[::-1]))
>>> l
[[1, 0, 0], [2, 0, 0], [3, 0, 0], [4, 0, 0], [0, 1, 0], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 0, 4], [1, 1, 0], [2, 1, 0], [3, 1, 0], [1, 2, 0], [2, 2, 0], [1, 3, 0], [1, 0, 1], [2, 0, 1], [3, 0, 1], [0, 1, 1], [0, 2, 1], [0, 3, 1], [1, 0, 2], [2, 0, 2], [0, 1, 2], [0, 2, 2], [1, 0, 3], [0, 1, 3], [1, 1, 1], [2, 1, 1], [1, 2, 1], [1, 1, 2]]
See http://docs.python.org/2/howto/sorting.html for an explanation of the Python sort capability, including a discussion of "Key Functions" which gives you whatever additional flexibility you need beyond the basic sort.
Use your sorting criteria function as key in the sorting.
input_list = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0], [0, 0, 3], [0, 1, 2], [0, 2, 1], [0, 3, 0], [1, 0, 2], [1, 1, 1], [1, 2, 0], [2, 0, 1], [2, 1, 0], [3, 0, 0], [0, 0, 4], [0, 1, 3], [0, 2, 2], [0, 3, 1], [0, 4, 0], [1, 0, 3], [1, 1, 2], [1, 2, 1], [1, 3, 0], [2, 0, 2], [2, 1, 1], [2, 2, 0], [3, 0, 1], [3, 1, 0], [4, 0, 0]]
sorted_list = sorted(input_list,key=my_sorting_func)

Categories

Resources