Python Turtle -- 尝试更新一个数组值会更新同一列表索引的所有值
Python Turtle -- Trying to update one array value updates all the values of the same list index
我正在编写一个程序,根据输入的行数和列数绘制正方形网格,获取输入的行、列和颜色索引(来自颜色查找的索引 table ), 然后用指定的颜色和位置在网格上绘制另一个正方形。循环直到输入空值。
我为网格的颜色索引值创建了一个位图数组,每个嵌套列表代表一行正方形,每个正方形的初始值设置为 0。
我想在每次绘制新的颜色方块时更新数组中的单个值。
我尝试使用
更新数组中的值
bitmap[row_index][col_index] = clut_index
在输入循环中,还有
bitmap[row][col] = colorindex
在绘制彩色方块的函数中。
每次,它不是更新数组中的一个值,而是使用相同的列表索引更新数组中的所有值,因此每个 row/list 最终都具有相同的值。
例如,
的输入
5 6
网格大小,
0 2 3
2 5 1
4 0 5
彩色方块的结果应该是
[[0,0,3,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0, 1], [0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0]]
但最终结果为
[[5,0,3,0,0,1],[5,0,3,0,0,1],[5,0,3,0,0, 1], [5, 0, 3, 0, 0, 1], [5, 0, 3, 0, 0, 1]]
方块在网格上绘制得很好,但位图列表没有正确更新。
如何一次只更新数组中的一个值?这是我的代码,我将有关位图数组的部分加粗了。很抱歉程序又长又乱:
from turtle import *
from array import *
#inputting the number of rows and columns to make the grid
grid_dimensions = input("Please input the number of rows and number of columns, separated by a space:")
grid_list = grid_dimensions.split(" ")
rows = int(grid_list[0])
columns = int(grid_list[1])
# setting the window size as an aspect ratio of the rows and columns
# this is to make sure that each grid cell is a square
# window has a max size of 800 pixels
if rows > columns:
window_w = (800/rows)*columns
window_h = 800
elif rows < columns:
window_w = 800
window_h = (800/columns)*rows
else:
window_w = 800
window_h = 800
# creating window and coordinate grid
# coordinates are in pixels, grid is the same size as the window
setup(window_w, window_h)
setworldcoordinates(0, 0, window_w, window_h)
# setting the side length of the square as the size of each grid cell
square_length = window_w/columns
# creating a color look-up table of RGB color values
clut = [(1.0, 1.0, 1.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), (0.0, 1.0, 1.0), (0.0, 0.0, 1.0), (1.0, 0.0, 1.0)]
# creating a bitmap array of the color index value for each square in the grid
# populating the array by setting each value to zero
rowline = []
bitmap = []
for col in range(columns):
rowline.append(0)
for row in range(rows):
bitmap.append(rowline)
# moves the pen to the starting position of each square
def movepos(x, y):
penup()
setpos(x, y)
pendown()
# draws the square based on the row and column positions on the grid
# uses the static square size
def drawsquare(row, col, square_size):
movepos(col*square_size, row*square_size)
for i in range(4):
forward(square_size)
left(90)
# calls the function to draw a square in a loop for the number of columns and rows
def drawgrid(row_number, col_number, square_size):
for row in range(row_number):
for col in range(col_number):
drawsquare(row, col, square_size)
# "colors in the grid cell" by drawing a colored square at a specific row and column position
def colorpixel(row, col, colorindex, square_size):
fillcolor(clut[colorindex])
begin_fill()
drawsquare(row, col, square_size)
end_fill()
# drawing the initial empty grid
drawgrid(rows, columns, square_length)
# creates an empty list to put the input values into
# creates a starting value for the loop
# takes the first set of row, column, and color input values to draw a colored square
index_input = input("Please input a row number, a column number, and a color number separated by spaces, then hit Enter. To finish coloring, hit Enter without entering any values: ")
# loops to draw multiple colored squares
# loop breaks when an empty value is entered
while index_input:
input_list = index_input.split(" ")
row_index = int(input_list[0])
col_index = int(input_list[1])
clut_index = int(input_list[2])
bitmap[row_index][col_index] = clut_index
colorpixel(row_index, col_index, clut_index, square_length)
index_input = input("Please input another row number, a column number, and a color number separated by spaces, then hit Enter. To finish coloring, hit Enter without entering any values: ")
#prints the bitmap array
print(bitmap)
done()
我认为问题出在这里:
for row in range(rows):
bitmap.append(rowline)
矩阵中的每一行都是相同列表,我们需要使它们成为唯一列表:
for row in range(rows):
bitmap.append(list(rowline)) # force a copy
这不是 Python 乌龟问题,而是一般的 Python 编程问题。
我正在编写一个程序,根据输入的行数和列数绘制正方形网格,获取输入的行、列和颜色索引(来自颜色查找的索引 table ), 然后用指定的颜色和位置在网格上绘制另一个正方形。循环直到输入空值。
我为网格的颜色索引值创建了一个位图数组,每个嵌套列表代表一行正方形,每个正方形的初始值设置为 0。
我想在每次绘制新的颜色方块时更新数组中的单个值。
我尝试使用
更新数组中的值bitmap[row_index][col_index] = clut_index
在输入循环中,还有
bitmap[row][col] = colorindex
在绘制彩色方块的函数中。
每次,它不是更新数组中的一个值,而是使用相同的列表索引更新数组中的所有值,因此每个 row/list 最终都具有相同的值。
例如,
的输入5 6
网格大小,
0 2 3
2 5 1
4 0 5
彩色方块的结果应该是
[[0,0,3,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0, 1], [0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0]]
但最终结果为
[[5,0,3,0,0,1],[5,0,3,0,0,1],[5,0,3,0,0, 1], [5, 0, 3, 0, 0, 1], [5, 0, 3, 0, 0, 1]]
方块在网格上绘制得很好,但位图列表没有正确更新。
如何一次只更新数组中的一个值?这是我的代码,我将有关位图数组的部分加粗了。很抱歉程序又长又乱:
from turtle import *
from array import *
#inputting the number of rows and columns to make the grid
grid_dimensions = input("Please input the number of rows and number of columns, separated by a space:")
grid_list = grid_dimensions.split(" ")
rows = int(grid_list[0])
columns = int(grid_list[1])
# setting the window size as an aspect ratio of the rows and columns
# this is to make sure that each grid cell is a square
# window has a max size of 800 pixels
if rows > columns:
window_w = (800/rows)*columns
window_h = 800
elif rows < columns:
window_w = 800
window_h = (800/columns)*rows
else:
window_w = 800
window_h = 800
# creating window and coordinate grid
# coordinates are in pixels, grid is the same size as the window
setup(window_w, window_h)
setworldcoordinates(0, 0, window_w, window_h)
# setting the side length of the square as the size of each grid cell
square_length = window_w/columns
# creating a color look-up table of RGB color values
clut = [(1.0, 1.0, 1.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), (0.0, 1.0, 1.0), (0.0, 0.0, 1.0), (1.0, 0.0, 1.0)]
# creating a bitmap array of the color index value for each square in the grid
# populating the array by setting each value to zero
rowline = []
bitmap = []
for col in range(columns):
rowline.append(0)
for row in range(rows):
bitmap.append(rowline)
# moves the pen to the starting position of each square
def movepos(x, y):
penup()
setpos(x, y)
pendown()
# draws the square based on the row and column positions on the grid
# uses the static square size
def drawsquare(row, col, square_size):
movepos(col*square_size, row*square_size)
for i in range(4):
forward(square_size)
left(90)
# calls the function to draw a square in a loop for the number of columns and rows
def drawgrid(row_number, col_number, square_size):
for row in range(row_number):
for col in range(col_number):
drawsquare(row, col, square_size)
# "colors in the grid cell" by drawing a colored square at a specific row and column position
def colorpixel(row, col, colorindex, square_size):
fillcolor(clut[colorindex])
begin_fill()
drawsquare(row, col, square_size)
end_fill()
# drawing the initial empty grid
drawgrid(rows, columns, square_length)
# creates an empty list to put the input values into
# creates a starting value for the loop
# takes the first set of row, column, and color input values to draw a colored square
index_input = input("Please input a row number, a column number, and a color number separated by spaces, then hit Enter. To finish coloring, hit Enter without entering any values: ")
# loops to draw multiple colored squares
# loop breaks when an empty value is entered
while index_input:
input_list = index_input.split(" ")
row_index = int(input_list[0])
col_index = int(input_list[1])
clut_index = int(input_list[2])
bitmap[row_index][col_index] = clut_index
colorpixel(row_index, col_index, clut_index, square_length)
index_input = input("Please input another row number, a column number, and a color number separated by spaces, then hit Enter. To finish coloring, hit Enter without entering any values: ")
#prints the bitmap array
print(bitmap)
done()
我认为问题出在这里:
for row in range(rows):
bitmap.append(rowline)
矩阵中的每一行都是相同列表,我们需要使它们成为唯一列表:
for row in range(rows):
bitmap.append(list(rowline)) # force a copy
这不是 Python 乌龟问题,而是一般的 Python 编程问题。