2D List - Python - weird loop behaviour (does not stop when told to) - python-3.x

My having troubles understanding why is my code not working like I think it should.
This function is supposed to fill up a 2 dimensional list with 1 instead of 0 given some parameters:
x = to tell it where to start to fill up on the x axis
y = to tell it where to start to fill up on the y axis
length = length of the rectangle
width = width of the rectangle
rotation = boolean to tell if the rectangle should be drawn
vertically (if True) or horizontally (if False)
So I call drawLoop() that will call drawVertical() that calls drawHorizon() afterward
This function is aimed at receiving multiple rectangle, but my problem lies when the first is added.
Here is the code:
# Create a 2D list of 120 * 60
thisBox = [["0"] * 120] * 60
# Filler function
def drawLoop(x, y, length, width, rotation):
def drawHorizon(x, y, length, width, rotation, row):
drawIndexH = 0
while drawIndexH < len(row):
if rotation == False and drawIndexH >= x and drawIndexH < x + length:
row[drawIndexH] = "1"
drawIndexH += 1
elif rotation == True and drawIndexH >= x and drawIndexH < x + width:
row[drawIndexH] = "1"
drawIndexH += 1
else:
return
def drawVertical(x, y, length, width, rotation):
drawIndexV = 0
while drawIndexV < len(thisBox):
if rotation == False and drawIndexV >= y and drawIndexV < y + width:
drawHorizon(x, y, length, width, rotation, thisBox[drawIndexV])
drawIndexV += 1
elif rotation == True and drawIndexV >= y and drawIndexV < y + length:
drawHorizon(x, y, length, width, rotation, thisBox[drawIndexV])
drawIndexV += 1
else:
drawIndexV += 1
# Launch vertical drawing
drawVertical(x, y, length, width, rotation)
# Launch main function
drawLoop(0, 0, 70, 50, False)
As of now, the 120 * 60 space is empty, so by calling the main function with drawloop(0, 0, 70, 50, False)on the last line, I'm supposed to see a 70 * 50 rectangle drawn at the position (0, 0). So that out of the 7200 0 (120 * 60) I should see only 3700 left (7200 - (70 * 50))
So the function is divided into 2 other functions: drawVertical(x, y, length, width, rotation) that will draw vertically and drawHorizon(x, y, length, width, rotation, row) that will draw horizontally first.
But somehow, at the first iteration of drawVertical(...), all the rows are being filled up in one iteration and do not stop at the exit condition: it should stop at y = 50 (the width) when if rotation == False and drawIndexV >= y and drawIndexV < y + width: of drawVertical(...) because drawIndexV < y + width should stop at 50. But it does not and I have no clue why.
Even if I tell drawVertical(...) to stop the loop at the first iteration with while drawIndexV < 2:, all the rows are being filled up.
So horizontally I have the expected result, but I never have it vertically. Can anybody spot my mistake? Many thanks in advance!
Ben.

The problem is indeed that
thisBox = [["0"] * 120] * 60
creates a list with 60 times the same element, a list with 120 "0", as the following code snippet shows:
for r in range(60):
print(id(thisBox[r]))
for which a sample output is:
140706579889408
140706579889408
140706579889408
140706579889408
140706579889408
140706579889408
...
Updating any element on any row will update the same element on every row, since every row is the same unique list object.
To avoid the issue, one needs to ensure that each of the enclosed lists (the 60 lists, each of which contains 120 "0") is a separate list object, distinct from all the other enclosed lists, i.e., has its own id.
If familiar with numpy (numpy.zeros), and depending on the exact requirements, resorting to numpy arrays could be a solution. If using numpy is an instance of "shooting a bird with a cannonball", one alternative is using list comprehensions to initialise the list:
thisBox = [["0" for c in range(120)] for r in range(60)]
Running the same code as before confirms that each list of 120 "0" now has its own id, i.e., each row is a separate list:
for r in range(60):
print(id(thisBox[r]))
140185522518784
140185522481600
140185522519680
140185522482560
140185503364672
...
(Would have added the solution described above, using list comprehensions, as a comment, but was barred from doing so because of insufficient "reputation" points)

I have found my problem (Merci Philippe!!!)
When I do this:
# Create a 2D list of 120 * 60
thisBox = [["0"] * 120] * 60
I'm actually creating a list of the same element 60 times. They all have the same memory allocation. So if I modify one, I modify them all. That's why one iteration of drawVertical(...) modified the whole 60 rows.

Related

Trouble understanding modulus on a negative in 'Conways Game of Life'

I'm going over the book 'automate the boring stuff with python' and cannot understanding a simple expression with the % operator. The expression is leftCoord = (x - 1) % WIDTH which on the first iteration of the loop evaluates to (0 - 1) % 60. In my mind the % operator should evaluate to the remainder of a division. Why does it evaluate to 9?
This is the part of the program that precedes the expression in question:
import random,time,copy
WIDTH = 60
HEIGHT = 20
# Create a list of list for the cells:
nextCells = []
for x in range(WIDTH):
column = [] # Create a new column.
for y in range(HEIGHT):
if random.randint(0,1) == 0:
column.append('#') # Add a living cell.
else:
column.append(' ') # Add a dead cell.
nextCells.append(column) # nextCells is a list of column lists.
while True: # Main program loop.
print('\n\n\n\n\n') # Separate each step with newlines.
currentCells = copy.deepcopy(nextCells)
# Print currentCells on the screen:
for y in range(HEIGHT):
for x in range(WIDTH):
print(currentCells[x][y], end='') # Print the # or space.
print() # Print a newline at the end of the row.
# Calculate the next step's cells based on current step's cells:
for x in range(WIDTH):
for y in range(HEIGHT):
# Get neighboring coordinates:
# % WIDTH ensures leftCoord is always between 0 and WIDTH -1
leftCoord = (x - 1) % WIDTH
rightCoord = (x + 1) % WIDTH
aboveCoord = (y - 1) % HEIGHT
belowCoord = (y + 1) % HEIGHT
For the sake of example, let's assume that you're using a table of 10x10.
The % operator isn't so intuitive when the first number is smaller than the second. Try going into the interactive python shell and running 4 % 10. Try 8 % 10. Notice how you always get the same number back? That's because the answer to the division is 0... with your whole number being left over as remainder. For most numbers in the table, the modulus doesn't do anything at all.
Now try -1 % 10 (simulating what this would do for the top row). It gives you 9, indicating the bottom row. If you run 10 % 10 (simulating the bottom row), it gives you 0, indicating the top row. Effectively, this makes the table "wrap"... the cells in the top row affect the bottom and vice versa. It also wraps around the sides.
Hope this helps!

Creating a symmetrical grid of random size squares in Python3/Tkinter

I have a question revolving around what would be a viable approach to placing out random-sized squares on a symmetrical, non-visible grid on a tkinter-canvas. I'm going to explain it quite thoroughly as it's a somewhat proprietary problem.
This far I've tried to solve it mostly mathematically. But I've found it to be quite a complex problem, and it seems reasonable that there would be a better approach to take it on than what I've tried.
In its most basic form the code looks like this:
while x_len > canvas_width:
xpos = x_len + margin
squares[i].place(x=xpos, y=ypos)
x_len += square_size + space
i += 1
x_len is the total width of all the squares on a given row, and resets when exiting the while-loop (eg. when x_len > window width), among with xpos (the position on X), as well as altering Y-axis to create a new row.
When placing same-size squares it looks like this:
So far so good.
However when the squares are of random-size it looks like this (at best):
The core problem, beyond that the layout can be quite unpredictable, is that the squares aren't centered to the "invisible grid" - because there is none.
So to solve this I've tried an approach where I use a fixed distance and a relative distance based on every given square. This yields satisficing results for the Y-axis on the first row, but not on the X-axis, nor the following rows on Y.
See example (where first row is centered on Y, but following rows and X is not):
So with this method I'm using a per-square alteration in both Y- and X-axis, based on variables that I fetch from a list that contain widths for all of the generated squares.
In it's entirety it looks like this (though it's work in progress so it's not very well optimized):
square_widths = [60, 75, 75, 45...]
space = square_size*0.5
margin = (square_size+space)/2
xmax = frame_width - margin - square_size
xmin = -1 + margin
def iterate(ypos, xpos, x_len):
y = ypos
x = xpos
z = x_len
i=0
m_ypos = 0
extra_x = 0
while len(squares) <= 100:
n=-1
# row_ypos alters y for every new row
row_ypos += 200-square_widths[n]/2
# this if-statement is not relevant to the question
if x < 0:
n=0
xpos = x
extra_x = x
x_len = z
while x_len < xmax:
ypos = row_ypos
extra_x += 100
ypos = row_ypos + (200-square_widths[n])/2
xpos = extra_x + (200-square_widths[n])/2
squares[i].place(x=xpos, y=ypos)
x_len = extra_x + 200
i += 1
n += 1
What's most relevant here is row_ypos, that alters Y for each row, as well as ypos, that alters Y for each square (I don't have a working calculation for X yet). What I would want to achieve is a similar result that I get for Y-axis on the first row; on all rows and columns (eg. both in X and Y). To create a symmetrical grid with squares of different sizes.
So my questions are:
Is this really best practice to solve this?
If so - Do you have any tips on decent calculations that would do the trick?
If not - How would you approach this?
A sidenote is that it has to be done "manually" and I can not use built-in functions of tkinter to solve it.
Why don't you just use the grid geometry manager?
COLUMNS = 5
ROWS = 5
for i in range(COLUMNS*ROWS):
row, col = divmod(i, COLUMNS)
l = tk.Label(self, text=i, font=('', randint(10,50)))
l.grid(row=row, column=col)
This will line everything up, but the randomness may make the rows and columns different sizes. You can adjust that with the row- and columnconfigure functions:
import tkinter as tk
from random import randint
COLUMNS = 10
ROWS = 5
class GUI(tk.Frame):
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
labels = []
for i in range(COLUMNS*ROWS):
row, col = divmod(i, COLUMNS)
l = tk.Label(self, text=i, font=('', randint(10,50)))
l.grid(row=row, column=col)
labels.append(l)
self.update() # draw everything
max_width = max(w.winfo_width() for w in labels)
max_height = max(w.winfo_height() for w in labels)
for column in range(self.grid_size()[0]):
self.columnconfigure(col, minsize=max_width) # set all columns to the max width
for row in range(self.grid_size()[1]):
self.rowconfigure(row, minsize=max_height) # set all rows to the max height
def main():
root = tk.Tk()
win = GUI(root)
win.pack()
root.mainloop()
if __name__ == "__main__":
main()
I found the culprit that made the results not turn out the way expected, and it wasn't due to the calculations. Rather it turned out that the list I created didn't put the squares in correct order (which I should know since before).
And so I fetched the width from the raw data itself, which makes a lot more sense than creating a list.
The function now looks something like this (again, it's still under refinement, but I just wanted to post this, so that people don't waste their time in coming up with solutions to an already solved problem :)):
def iterate(ypos, xpos, x_len):
y = ypos
x = xpos
z = x_len
i=0
while len(squares) <= 100:
n=0
if y > 1:
ypos -= max1 + 10
if y < 0:
if ypos < 0:
ypos=10
else:
ypos += max1 + 10 #+ (max1-min1)/2
if x < 0:
n=0
xc=0
xpos = x
x_len = z
while x_len < xmax:
yc = ypos + (max1-squares[i].winfo_width())/2
if xpos <= 0:
xpos = 10
else:
xpos += max1 + 10
xc = xpos + (max1-squares[i].winfo_width())/2
squares[i].place(x=xc, y=yc)
x_len += max1 + 10
print (x_len)
i += 1
n += 1

Infinite loops for turtle listen

I am running into an infinite loop in this code. It should break out if you click in the desired range, however it goes into an infinite loop displaying the current position of the turtle in the row and column format.
def wait_for_click():
turt.penup()
wn.onclick(turt.goto)
wn.listen()
pos = [-1,-1]
row = -1
column = -1
while row > 8 or row < 0 or column > 8 or column < 0:
row = ((turt.ycor()-turt.ycor()%75)+75)/75 + 4
column = ((turt.xcor()-turt.xcor()%75)+75)/75 + 4
pos[0] = row
pos[1] = column
print(pos)
I think your basic approach is wrong: don't loop waiting for the turtle to show up somewhere interesting, instead use the click handler to test where the turtle showed up:
from math import ceil
from turtle import Turtle, Screen
CELLS = 8
CELL_SIZE = 75
STAMP_SIZE = 20
def click_handler(x, y):
screen.onclick(None)
yertle.goto(x, y)
if 0 < x < CELLS and 0 < y < CELLS:
position = [ceil(x), ceil(y)]
print(position)
screen.onclick(click_handler)
screen = Screen()
screen.setup(CELL_SIZE * (CELLS + 2), CELL_SIZE * (CELLS + 2))
screen.setworldcoordinates(-1, -1, CELLS + 1, CELLS + 1)
screen.onclick(click_handler)
marker = Turtle(shape="square")
marker.penup()
marker.turtlesize(CELL_SIZE / STAMP_SIZE)
marker.color("gray", "white")
for x in range(0, CELLS):
for y in range(0, CELLS):
marker.goto(x + 0.5, y + 0.5)
marker.stamp()
marker.color(*marker.color()[::-1])
marker.color(*marker.color()[::-1])
yertle = Turtle(shape="circle")
yertle.speed("fastest")
yertle.penup()
screen.mainloop()
I've thrown in code to show the grid so you can see that where you click matches the printed output. I used setworldcoordinates() to simplify the problem with the side effect that I've given you a larger border.
The lower left cell is [1, 1] and the upper right is [8, 8] -- you may want to do some math to switch these around.

I want to create a 90 degree curve

I have gotten as far as making a set of rays, but I need to connect them. Any help? My code is as follows
from math import *
from graphics import *
i = 1
segments = 15
lastPoint = Point(100,0)
print("Begin")
win = GraphWin("Trigonometry", 1500, 1500)
while i<=segments:
angle =i*pi/segments
y = int(sin(angle)*100)
x = int(cos(angle)*100)
i = i+1
p = Point(x,y)
l = Line(p, lastPoint)
l.draw(win)
print(p.x, p.y)
print("End")
OP code draws only "rays" because, while point p lays on the circle, lastPoint doesn't change between iterations.
We have to update the value of lastPoint to literally the last point calculated in order to draw the arc as a series of consecutive segments.
Here is a modified code, with further explanations as asked by OP in his comment:
from math import *
from graphics import *
# Function to calculate the integer coordinates of a Point on a circle
# given the center (c, a Point), the radius (r) and the angle (a, radians)
def point_on_circle( c, r, a ) :
return Point( int(round(c.x + r*cos(a))), int(round(c.y + r*sin(a))) )
# Define the graphical output window, I'll set the coordinates system so
# that the origin is the bottom left corner of the windows, y axis is going
# upwards and 1 unit corresponds to 1 pixel
win = GraphWin("Trigonometry", 800, 600)
win.setCoords(0,0,800,600)
# Arc data. Angles are in degrees (more user friendly, but later will be
# transformed in radians for calculations), 0 is East, positive values
# are counterclockwise. A value of 360 for angle_range_deg gives a complete
# circle (polygon).
angle_start_deg = 0
angle_range_deg = 90
center = Point(10,10)
radius = 200
segments = 16
angle_start = radians(angle_start_deg)
angle_step = radians(angle_range_deg) / segments
# Initialize lastPoint with the position corresponding to angle_start
# (or i = 0). Try different values of all the previous variables
lastPoint = point_on_circle(center, radius, angle_start)
print("Begin")
i = 1
while i <= segments :
# update the angle to calculate a new point on the circle
angle = angle_start + i * angle_step
p = point_on_circle(center, radius, angle)
# draw a line between the last two points
l = Line(p, lastPoint)
l.draw(win)
print(p.x, p.y)
# update the variables to move on to the next segment which share an edge
# (the last point) with the previous segment
i = i + 1
lastPoint = p
print("End")

Selecting colors that are furthest apart

I'm working on a project that requires me to select "unique" colors for each item. At times there could be upwards of 400 items. Is there some way out there of selecting the 400 colors that differ the most? Is it as simple as just changing the RGB values by a fixed increment?
You could come up with an equal distribution of 400 colours by incrementing red, green and blue in turn by 34.
That is:
You know you have three colour channels: red, green and blue
You need 400 distinct combinations of R, G and B
So on each channel the number of increments you need is the cube root of 400, i.e. about 7.36
To span the range 0..255 with 7.36 increments, each increment must be about 255/7.36, i.e. about 34
Probably HSL or HSV would be a better representations than RGB for this task.
You may find that changing the hue gives better variability perception to the eye, so adjust your increments in a way that for every X units changed in S and L you change Y (with Y < X) units of hue, and adjust X and Y so you cover the spectrum with your desired amount of samples.
Here is my final code. Hopefully it helps someone down the road.
from PIL import Image, ImageDraw
import math, colorsys, os.path
# number of color circles needed
qty = 400
# the lowest value (V in HSV) can go
vmin = 30
# calculate how much to increment value by
vrange = 100 - vmin
if (qty >= 72):
vdiff = math.floor(vrange / (qty / 72))
else:
vdiff = 0
# set options
sizes = [16, 24, 32]
border_color = '000000'
border_size = 3
# initialize variables
hval = 0
sval = 50
vval = vmin
count = 0
while count < qty:
im = Image.new('RGBA', (100, 100), (0, 0, 0, 0))
draw = ImageDraw.Draw(im)
draw.ellipse((5, 5, 95, 95), fill='#'+border_color)
r, g, b = colorsys.hsv_to_rgb(hval/360.0, sval/100.0, vval/100.0)
r = int(r*255)
g = int(g*255)
b = int(b*255)
draw.ellipse((5+border_size, 5+border_size, 95-border_size, 95-border_size), fill=(r, g, b))
del draw
hexval = '%02x%02x%02x' % (r, g, b)
for size in sizes:
result = im.resize((size, size), Image.ANTIALIAS)
result.save(str(qty)+'/'+hexval+'_'+str(size)+'.png', 'PNG')
if hval + 10 < 360:
hval += 10
else:
if sval == 50:
hval = 0
sval = 100
else:
hval = 0
sval = 50
vval += vdiff
count += 1
Hey I came across this problem a few times in my projects where I wanted to display, say, clusters of points. I found that the best way to go was to use the colormaps from matplotlib (https://matplotlib.org/stable/tutorials/colors/colormaps.html) and
colors = plt.get_cmap("hsv")[np.linspace(0, 1, n_colors)]
this will output rgba colors so you can get the rgb with just
rgb = colors[:,:3]

Resources