get lines where value does not exist - python

I'm trying to get from 2D list the lines where value = 2 does not exists:
[[0, 0, 1, 1],
[2, 1, 0, 2],
[2, 2, 2, 2],
[0, 0, 1, 0]]
I've been using:
for i in range(len(list)):
if list[i] in [list]:
# dot stuff
the problem here is that evrytime the code is using the actual line which is list[i] and ignore the the rest even if 2 does not exists on it
hope Iwas clear
Thank you

something like the below
lst = [[0, 0, 1, 1],
[2, 1, 0, 2],
[2, 2, 2, 2],
[0, 0, 1, 0]]
lst = [x for x in lst if 2 not in x]
print(lst)
output
[[0, 0, 1, 1], [0, 0, 1, 0]]

Is this your expected output?
[0, 0, 1, 1]
[0, 0, 1, 0]
This is the working code:
a = [[0, 0, 1, 1],
[2, 1, 0, 2],
[2, 2, 2, 2],
[0, 0, 1, 0]]
for i in range(len(a)):
if 2 not in a[i]:
print(a[i])

Related

Create product of lists, concatenated, in python

import itertools
x = [[0,0],[1,1]]
list(itertools.product(x,x))
produces
[([0, 0], [0, 0]), ([0, 0], [1, 1]), ([1, 1], [0, 0]), ([1, 1], [1, 1])]
But I'm looking for something that produces
[[0, 0, 0, 0], [0, 0, 1, 1], [1, 1, 0, 0], [1, 1, 1, 1]]
itertools.product is giving you that answer, you just have to concatenate the lists
[a + b for a, b in [([0, 0], [0, 0]), ([0, 0], [1, 1]), ([1, 1], [0, 0]), ([1, 1], [1, 1])]]
# [[0, 0, 0, 0], [0, 0, 1, 1], [1, 1, 0, 0], [1, 1, 1, 1]]
In that case, you can easily do without using itertools, using list comprehension:
x = [[0, 0], [1, 1]]
output = [a + b for b in x for a in x]
# [[0, 0, 0, 0], [1, 1, 0, 0], [0, 0, 1, 1], [1, 1, 1, 1]]
the equivalent without list comprehension would be:
output = []
for a in x:
for b in x:
output.append(a + b)
print(output)
You can use np.reshape if you are considering numpy array.
np.reshape(list(itertools.product(x,x)),(4,4))
Out[28]:
array([[0, 0, 0, 0],
[0, 0, 1, 1],
[1, 1, 0, 0],
[1, 1, 1, 1]])

Algorithm to create a binary-like pattern

I have two numbers: n and sp. I want to create a pattern of list elements with sp elements that rotate like binary digits up to n. To elaborate more on the second part, here is an example:
n = 2, sp = 3
[0, 0, 0]
[0, 0, 1]
[0, 1, 0]
[0, 1, 1]
[1, 0, 0]
[1, 0, 1]
[1, 1, 0]
[1, 1, 1]
Similarly:
n = 3, sp = 3
[0, 0, 0]
[0, 0, 1]
[0, 0, 2]
[0, 1, 0]
[0, 2, 0]
[0, 1, 1]
[0, 1, 2]
[0, 2, 1]
[0, 2, 2]
[1, 0, 0]
[1, 0, 1]
[1, 0, 2]
[1, 1, 0]
[1, 2, 0]
[1, 1, 1]
[1, 1, 2]
[1, 2, 1]
[1, 2, 2]
[2, 0, 0]
[2, 0, 1]
[2, 0, 2]
[2, 1, 0]
[2, 2, 0]
[2, 1, 1]
[2, 1, 2]
[2, 2, 1]
[2, 2, 2]
I wish to maintain the order that I have given in the examples, that is the LSB is assigned first, then the next bit, and so on...
I could not think of any tactic to solve such a problem. All I could figure out is to use sp to create a temporary list of size sp, for instance, if sp == 3, the temp_list can be [0 0 0]. Then we iterate in a manner so that we get the pattern.
There is no constraint on the complexity of the algorithm, although a shorter algorithm would be very great.
One simple way of doing it is:
def get_patterns(n, sp):
ans = []
def _get_patterns(so_far):
if len(so_far) == sp:
ans.append(so_far[:])
return
for i in range(n):
so_far.append(i)
_get_patterns(so_far)
so_far.pop()
_get_patterns([])
return ans
n = 3
sp = 3
assert get_patterns(n, sp) == sorted([
[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0],
[0, 2, 0], [0, 1, 1], [0, 1, 2], [0, 2, 1], [0, 2, 2],
[1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 1, 0],
[1, 2, 0], [1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 2, 2],
[2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 1, 0],
[2, 2, 0], [2, 1, 1], [2, 1, 2], [2, 2, 1], [2, 2, 2],
])
I wrote this
n = 4 # base
sp = 2 # length
def fix(num, base):
for j in range(len(num)):
if num[j] == base:
num[j] = 0
num[j+1] += 1
return num
num= [ 0 for _ in range(sp)]
print(num)
for i in range(n**sp -1):
num[0] += 1
num= fix(num, n)
print(num[::-1])

Python - two-dimensional array adressing fills up the whole array

I have a problem. First let me show you the code:
def neon(l):
oc = 0; #naj ocena
tz = 0; #teraz ocena
#sprawdzanie poziomo
for x in range(len(l[0])):
for i in range(len(l[x])):
for y in range(i + 1,len(l[x])):
tz = l[x][i] + l[x][y] + (max(y - x, x - y) + 1) * 2;
if (tz > oc):
oc = tz;
pion = [[0] * len(l[0])] * len(l);
print(pion);
print("#######");
for i in range(len(pion)):
for y in range(len(pion[i])):
pion[i][y] = l[y][i];
print(pion);
neon([[1,2,1,2],[7,1,7,1],[1,1,1,1],[3,3,3,3]]);
The problem is that when i try to adress pion[i][y] instead of just changing that value from 0 to whatever the program changes the value in all of the inner arrays with the second index y. This is how it looks:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
#######
[[1, 7, 1, 3], [1, 7, 1, 3], [1, 7, 1, 3], [1, 7, 1, 3]]
[[2, 1, 1, 3], [2, 1, 1, 3], [2, 1, 1, 3], [2, 1, 1, 3]]
[[1, 7, 1, 3], [1, 7, 1, 3], [1, 7, 1, 3], [1, 7, 1, 3]]
[[2, 1, 1, 3], [2, 1, 1, 3], [2, 1, 1, 3], [2, 1, 1, 3]]
Instead, it should be:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
#######
[[1, 7, 1, 3], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[1, 7, 1, 3], [2, 1, 1, 3], [0, 0, 0, 0], [0, 0, 0, 0]]
[[1, 7, 1, 3], [2, 1, 1, 3], [1, 7, 1, 3], [0, 0, 0, 0]]
[[1, 7, 1, 3], [2, 1, 1, 3], [1, 7, 1, 3], [2, 1, 1, 3]]
Please help and thank you in advance.
This problem occurs due to the following reasons:
pionis a list of inner lists. Each element of pion just holds the reference to one inner list.
Because of the way you have created and initialized pion (using pion = [[0] * len(l[0])] * len(l)), all elements of pion hold references to the same inner list. So, in pion, instead of many distinct inner lists, you just have multiple references to a single inner list. In other words, pion[0], pion[1], pion[2], etc, are all references to the same inner list of zeros. Any modification that you make to this inner list, using a particular row index (eg, using the expression pion[3]), will be visible through all the other row indexes also, because, at all row indexes, you are just holding a reference to the same inner list.
To correct this, you need to create and initialize the list differently. Eg, if rows and cols are respectively the number of rows and columns, you could do something like this:
pion = [([0]*cols) for i in range(rows)]

Transferring Values in nested dictionaries/lists

list = [[1, 2, 3, 0, 0], [0, 0, 3, 2, 1], [1, 0, 0, 3, 2],
[2, 3, 1, 0, 0], [3, 0, 1, 2, 0], [2, 0, 1, 3, 0]]
I would like to check if the number 1 is in the third column of all the nested lists, if it is than it should replace the 1 with a 0 and the 2 in that list with a 1.
Thanks in advance
Try the following:
nested_lists = [[1, 2, 3, 0, 0], [0, 0, 3, 2, 1], [1, 0, 0, 3, 2],
[2, 3, 1, 0, 0], [3, 0, 1, 2, 0], [2, 0, 1, 3, 0]]
for list_ in nested_lists:
if list_[2] == 1:
list_[2] = 0
list_ = [1 if n == 2 else n for n in list_]
After execution, nested_lists goes from the given
[[1, 2, 3, 0, 0], [0, 0, 3, 2, 1], [1, 0, 0, 3, 2],
[2, 3, 1, 0, 0], [3, 0, 1, 2, 0], [2, 0, 1, 3, 0]]
To
[[1, 2, 3, 0, 0], [0, 0, 3, 2, 1], [1, 0, 0, 3, 2]
[1, 3, 0, 0, 0], [3, 0, 0, 1, 0], [1, 0, 0, 3, 0]]

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)

Categories

Resources