Tkinter, Draughts board - python-3.x

I'd like to create a Draughts' game board.
At first, Everything worked fine while I was drawing squares and pawns directly on the "board" canvas. As I have to manipulate those pawns afterward, I wanted it to be more explicite by creating each square and pawn as objects.
So I tried to create a Board which will contain black or white square which will contain circles (the pawns). Then everything, messed up. I don't know why. Even so it looks perfectly logical (for me).
I think it is related to the use of the pack method.
Here's the code :
from tkinter import *
class Parent(Tk):
def getRoot(self):
return(self.body)
def setTitle(self,title):
self.title(title)
def run(self):
self.mainloop()
class Drawing:
def __init__(self,root,width,height):
self.zone=Canvas(root, width=width, height=height)
def put(self,root,row,col):
self.zone.grid(root,row=row,column=col)
def getDrawing(self):
return(self.zone)
def rectangle(self,coordX1,coordY1,coordX2,coordY2,color):
self.zone.create_rectangle(coordX1,coordY1,coordX2,coordY2,fill=color, outline="black")
def circle(self,coordX1,coordY1,coordX2,coordY2,color):
self.zone.create_oval(coordX1,coordY1,coordX2,coordY2,fill=color,outline="black")
if __name__=="__main__":
root=Parent()
root.setTitle("Draughts")
board=Drawing(root,400,400)
size=40
logicBoard=[[0, -1, 0, -1, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, 0, -1, 0, -1, 0, -1, 0],
[0, -1, 0, -1, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, 0, -1, 0, -1, 0, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]]
DIMENSION=10
for i in range(DIMENSION):
for j in range(DIMENSION):
coordX1 = (i * size)
coordY1 = (j * size)
coordX2 = coordX1 + size
coordY2 = coordY1 + size
if(not(i%2==j%2)):#if the square is black (on the board)
color="black"
else:
color="white"
case=Drawing(board.getDrawing(),40,40)
case.rectangle(coordX1,coordY1,coordX2,coordY2,color)
case.getDrawing().pack()
if(logicBoard[i][j]>0):
pawnColor="white"
elif(logicBoard[i][j]<0):
pawnColor="black"
if (not(i%2==j%2)):
pawn=Drawing(case.getDrawing(),40,40)
pawn.circle(0,0,30,30,pawnColor)
pawn.getDrawing().pack()
board.getDrawing().pack()
root.run()
Thank you !
EDIT:
This is what I get :

The problem is that you create a new Canvas and call pack() on it in each iteration, instead of using the one you create at the beginning. In the end, you are using the class Drawing to create new Drawings. I suggest you to use only one class to represent all the board, with the methods to draw the squares and the circles.
I've changed the colors of the ovals to have a better contrast:
from tkinter import Tk, Canvas
from itertools import product
class Board(Tk):
def __init__(self, width, height, cellsize):
Tk.__init__(self)
self.cellsize = cellsize
self.canvas = Canvas(self, width=width, height=height)
self.canvas.bind("<Button-1>", self.onclick)
self.canvas.pack()
def draw_rectangle(self, x1, y1, x2, y2, color):
self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
def draw_circle(self, x1, y1, x2, y2, color):
self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
def onclick(self, event):
i = int(event.x / self.cellsize)
j = int(event.y / self.cellsize)
print "You clicked on cell (%s, %s)" % (i, j)
if __name__=="__main__":
size = 40
board = Board(400, 400, size)
board.title("Draughts")
logicBoard = [[0, -1, 0, -1, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, 0, -1, 0, -1, 0, -1, 0],
[0, -1, 0, -1, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, 0, -1, 0, -1, 0, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]]
for (i, j) in product(range(10), range(10)):
coordX1 = (i * size)
coordY1 = (j * size)
coordX2 = coordX1 + size
coordY2 = coordY1 + size
color = "white" if i%2 == j%2 else "black"
board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, color)
cell = logicBoard[i][j]
if cell != 0:
pawnColor = "red" if cell > 0 else "blue"
board.draw_circle(coordX1, coordY1, coordX2, coordY2, pawnColor)
board.mainloop()
Edit:
If you want to keep track of the clicks, it is clearer and simpler to bind a handler function to the canvas and calculate the point, instead of creating lots of canvas with their own event handler.

Related

tensorflow Keras: Dimenions must be equal ValueError

I'm trying to train a model in Keras to suggest the best possible next move when presented with a pawn chess board. the board is represented as a list of 64 integers (0 for empty, 1 for player, 2 for enemy). The output is represented by a list of a field and a direction that the figure on that field should move in, which means I need two ouput layers with size 64 (number of fields) and 5 (number of possible move directions, including two forward and no move for when the game is over).
I have a list of boards and a list of solutions. When I try to fit the model however, I get the above mentioned error.
The exact error message is:
Epoch 1/75
Traceback (most recent call last):
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\main.py", line 75, in <module>
model.fit(train_fig_starts, train_fig_moves, epochs=75)
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\venv\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\lulll\AppData\Local\Temp\__autograph_generated_filej0zia4d5.py", line 15, in tf__train_function
retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
ValueError: in user code:
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\venv\lib\site-packages\keras\engine\training.py", line 1249, in train_function *
return step_function(self, iterator)
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\venv\lib\site-packages\keras\engine\training.py", line 1233, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\venv\lib\site-packages\keras\engine\training.py", line 1222, in run_step **
outputs = model.train_step(data)
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\venv\lib\site-packages\keras\engine\training.py", line 1024, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\venv\lib\site-packages\keras\engine\training.py", line 1082, in compute_loss
return self.compiled_loss(
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\venv\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\venv\lib\site-packages\keras\losses.py", line 152, in __call__
losses = call_fn(y_true, y_pred)
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\venv\lib\site-packages\keras\losses.py", line 284, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\venv\lib\site-packages\keras\losses.py", line 2176, in binary_crossentropy
backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
File "C:\Users\lulll\Documents\CodeStuff\tfTesting\venv\lib\site-packages\keras\backend.py", line 5688, in binary_crossentropy
bce = target * tf.math.log(output + epsilon())
ValueError: Dimensions must be equal, but are 2 and 64 for '{{node binary_crossentropy/mul}} = Mul[T=DT_FLOAT](binary_crossentropy/Cast, binary_crossentropy/Log)' with input shapes: [?,2], [?,64].
I have absolutely no idea what is causing this. I've searched for the error already, but the only mentions I've found seem to be describing a completely different scenario.
Since it probably helps, here's the code used to create and fit the model:
inputs = tf.keras.layers.Input(shape=64)
x = tf.keras.layers.Dense(32, activation='relu')(inputs)
out_field = tf.keras.layers.Dense(64, name="field")(x)
out_movement = tf.keras.layers.Dense(5, name="movement")(x)
model = tf.keras.Model(inputs=inputs, outputs=[out_field, out_movement])
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=['accuracy'])
model.fit(train_fig_starts, train_fig_moves, epochs=75) #train_fig_starts and moves are defined above
EDIT 1: Here's a sample of the dataset I'm using (the whole thing is too long for the character limit)
train_fig_starts = [[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 2, 0, 1, 0, 0, 0, 0, 1, 2, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0], [0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 1, 2, 2, 2, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 2, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 1, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]]
train_fig_moves = [[0, 0], [0, 0], [0, 0], [0, 0], [15, 2], [15, 2]]
EDIT 2:
I changed it to sparsecategorialcrossentropy since that seems more like what I'm looking for. This is now the model code
inputs = tf.keras.layers.Input(shape=64)
x = tf.keras.layers.Dense(64, activation='relu')(inputs)
out_field = tf.keras.layers.Dense(64, activation="relu", name="field")(x)
out_field = tf.keras.layers.Dense(64, activation="softmax", name="field_softmax")(out_field)
out_movement = tf.keras.layers.Dense(5, activation="relu", name="movement")(x)
out_movement = tf.keras.layers.Dense(5, activation="softmax", name="movement_softmax")(out_movement)
model = tf.keras.Model(inputs=inputs, outputs=[out_field, out_movement])
print(model.summary())
tf.keras.utils.plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
model.compile(optimizer='adam',
loss=[tf.keras.losses.SparseCategoricalCrossentropy(),
tf.keras.losses.SparseCategoricalCrossentropy()],
metrics=['accuracy'])
it still throws an error, this time its the following:
Node: 'sparse_categorical_crossentropy_1/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits'
logits and labels must have the same first dimension, got logits shape [32,5] and labels shape [64]
[[{{node sparse_categorical_crossentropy_1/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_1666]
I have no idea why its like that. Output logits and labels should both be [64, 2]. Since I'm using sparse crossentropy I should be able to use integers in my training data to signify the "index" of the ouput neuron with the highest logit, right? Correct me if I'm wrong. If it helps, here's a diagram of my model:
plot of the model
So I fixed the issue by myself now. Honestly it was a pretty stupid error to make but the error messages didn't really explain well what was going on. I swapped the outputs for one hot encoding and changed the loss to CategorialCrossEntropy, which is also more fitting for a categorisation problem (Sparse didn't work with my integers for some reason). After that I needed to change the label list from a 1dim list containing lists of len = 2 to a 2dim list containing both the field and the move one hots in a separate list. If anyone runs into a similar issue and can't make sense of it, maybe this will help.

Calculate the number of conenected pixels of a particular value in Numpy/OpenCV

def get_area(center_x: int, center_y: int, mask: np.ndarray) -> int:
if mask[center_x][center_y] != 255:
return -1
return ...
Now I got this function above that takes in a value for the x and y and finds the number of pixels that are connected to this pixel with the value of 255.
Now let's say, I have a simple np.ndarray that looks like this:
[
[255,255, 0, 0, 0, 0, 0,255,255],
[255, 0, 0,255,255,255, 0, 0,255],
[ 0, 0,255, 0, 0, 0,255, 0, 0],
[ 0,255, 0, 0,255, 0, 0,255, 0],
[ 0,255, 0,255,255,255, 0,255, 0],
[ 0,255, 0, 0,255, 0, 0,255, 0],
[ 0, 0,255, 0, 0, 0,255, 0, 0],
[255, 0, 0,255,255,255, 0, 0,255],
[255,255, 0, 0, 0, 0, 0,255,255]
]
If I took the center pixel of 255 as an input, the output of the function I am trying to build will be 5, since there are 4 neighboring pixels that are 255s.
I am amenable to using both opencv and numpy, but np is more preferable.
from PIL import Image
import numpy as np
from scipy import ndimage
imgMtx = [
[255,255, 0, 0, 0, 0, 0,255,255],
[255, 0, 0,255,255,255, 0, 0,255],
[ 0, 0,255, 0, 0, 0,255, 0, 0],
[ 0,255, 0, 0,255, 0, 0,255, 0],
[ 0,255, 0,255,255,255, 0,255, 0],
[ 0,255, 0, 0,255, 0, 0,255, 0],
[ 0, 0,255, 0, 0, 0,255, 0, 0],
[255, 0, 0,255,255,255, 0, 0,255],
[255,255, 0, 0, 0, 0, 0,255,255]
]
img = Image.fromarray(np.asarray(imgMtx))
blobs = np.asarray(img) > 125
labels, nlabels = ndimage.label(blobs)
unique, counts = np.unique(labels, return_counts=True)
import numpy as np
def get_area(center_x: int, center_y: int, mask: np.ndarray) -> int:
"""
Basic flood-fill area calculation on the mask using Breadth First Search
"""
area = 0
moves = [(1, 1), (0, 1), (1, 0), (-1, -1), (0, -1), (-1, 0), (-1, 1), (1, -1)]
if mask[center_x][center_y] != 255:
return -1
visited = [[False for _ in range(mask.shape[0])] for _ in range(mask.shape[1])]
q = deque()
q.append((center_x, center_y))
visited[center_x][center_y] = True
while q:
x, y = q.popleft()
for move in moves:
if (x + move[0] >= mask.shape[0] or y + move[1] >= mask.shape[1] or
x + move[0] < 0 or y + move[1] < 0):
continue
if mask[x + move[0]][y + move[1]] == 255 and not visited[x + move[0]][y + move[1]]:
area += 1
q.append((x + move[0], y + move[1]))
visited[x + move[0]][y + move[1]] = True
return area
Decided to code an answer by myself and made a breadth first search floodfill algorithm on the supplied mask.

Why does recursion not continue?

I have two list. first list is a maze. It start 'O' and must be finished 'X'.
maze_list=[[0, 1, 1, 0, 0, 0],
[0, 0, 1, 0, 1, 'O'],
[0, 0, 1, 0, 1, 0],
[0, 1, 1, 1, 1, 0],
['X', 1, 0, 1, 0, 0],
[0, 1, 0, 1, 1, 1]]
path_list=[[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
This part is finding start position.
column_size=len(maze_list)
row_size=len(maze_list[0])
def find_SF_position(X):
liste=[]
for i in range(column_size):
for j in range(column_size):
if maze_list[i][j]==X:
liste.append(i)
liste.append(j)
return liste
s_p=find_SF_position('O') #[1,5]
f_p=find_SF_position('X') #[4,0]
This part is main algorithm. x is for horizontal position. y is for vertical position
def solve_maze(maze,road,x,y):
global column_size
global row_size
road[y][x]=1
print(y,x)
#left
if x>0 and maze[y][x-1]==1 and road[y][x-1]==0:
return solve_maze(maze,road,x-1,y)
#up
if y>0 and maze[y-1][x]==1 and road[y][x-1]==0:
return solve_maze(maze,road,x,y-1)
#right
if x>row_size and maze[y][x+1]==1 and road[y][x-1]==0:
return solve_maze(maze,road,x+1,y)
#down
if y>column_size and maze[y+1][x]==1 and road[y][x-1]==0:
return solve_maze(maze,road,x,y+1)
if maze[x][y]=='X':
return path_list
solve_maze(maze_list,path_list,s_p[1],s_p[0])
print(path_list)
I can't understand why it isn't working. It's start 'O'(maze_list[1,5]), it fill maze_list[1,4] then stop.
Finally path_list must be like this
path_list=[[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 0],
[1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
You have a few problems in your code:
First your right and down conditions are reversed: x can never be greater than row_size. What you want to check is that x is not on the right border, meaning: x < row_size - 1. Same logic goes for the down case
Seems like you have a little copy-paste issue there: for example the road condition for up should be: road[y-1][x]==0. Same logic goes to all other cases.
Your condition to finding the end has a problematic logic: You check if the current cell is the 'X', but each step is only taken if there is 1 in the next cell, so how will you get to the 'X'? For example, after fixing above errors, the output I got was:
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 1]
[0, 0, 0, 0, 1, 0]
[0, 1, 1, 1, 1, 0]
[0, 1, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0]
As you can see, the 'X' was missed as the condition for down was taken at (4, 1) and a dead-end was reached
Lastly, what happens if you reach a dead-end? Your algorithm always takes the first step with conditions met, but what happens when you encounter an intersection? You need to check all possible paths. That's the point of recursion. The way you did it now doesn't add much over a loop.

Variational autoencoder - comparative analysis of code

Could you comment two version of variational autoencoder loss and show me why they give me different results?
Dataset:
data1 = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0], dtype='int32')
data2 = np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0], dtype='int32')
data3 = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0], dtype='int32')
100 samples each, so I have 300 samples.
Code 1:
def vae_loss(x, x_decoded_mean):
xent_loss = objectives.binary_crossentropy(x, x_decoded_mean)
kl_loss = -0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var))
loss = xent_loss + kl_loss
return loss
vae.compile(optimizer='rmsprop', loss=vae_loss)
Code 2:
def zero_loss(y_true, y_pred):
return K.zeros_like(y_pred)
class CustomVariationalLayer(Layer):
def __init__(self, **kwargs):
self.is_placeholder = True
super(CustomVariationalLayer, self).__init__(**kwargs)
def vae_loss(self, x, x_decoded_mean):
xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
K.exp(z_log_var), axis=-1)
return K.mean(xent_loss + kl_loss)
def call(self, inputs):
x = inputs[0]
x_decoded_mean = inputs[1]
loss = self.vae_loss(x, x_decoded_mean)
self.add_loss(loss, inputs=inputs)
return K.ones_like(x)
loss_layer = CustomVariationalLayer()([x, x_decoded_mean])
vae = Model(x, [loss_layer])
vae.compile(optimizer='rmsprop', loss=[zero_loss])
Results are so different and I don't see where? Latent dimension are different. Code 2 shows the separation between groups and code 1 not.
code 1, vae.predict... is not accurate and code 2 give me 1 on all features.
Code 2 gives me accurate feedback of the code:
sent_encoded = encoder.predict(np.array(test), batch_size = batch_size)
sent_decoded = generator.predict(sent_encoded)
and code 1 is not accurate at all.
Both experiments have the same layers. So, once again, where is the different and what is the best solution for dataset like described above?

python 3 comprehension dictionary

This is my code:
def brujinGraph(k, strList):
vertex = [[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0]]
brujinGraph = {strList[i]: strList[j][:-1] for i in range(len(vertex)) for j in range(k) and vertex[i][j] == 1}
return brujinGraph
strList = ['AAGA', 'AAGA', 'AGAT', 'ATTC', 'CTAA', 'CTCT', 'GATT', 'TAAG', 'TCTA', 'TCTC', 'TTCT']
brujinGraph(4, strList)
and it is throwing me an UnboundLocalError: local variable 'j' referenced before assignment
any idea what does it means and why am I getting this error?
Without knowing exactly what vertex and strList are :
Do you actually mean :
{strList[i]: strList[j][:-1] for i in range(len(vertex)) for j in range(len(vertex[i])) if vertex[i][j] == 1}
i.e. change that and into an if
Couple of issues:
You need an if not an and at the end
I think it is better expressed this way:
brujinGraph = {strList[i]: strList[j][:-1] for i, x in enumerate(vertex) for j, e in enumerate(x) if e == 1}

Resources