python - 制作数组的副本未按预期工作
问题描述
我有一个算法来制作地板的地图......
def floor_room_IDs():
global floor_roomIDs
rand_ID = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
r_y = 0
for Layer in floor_roomIDs:
r_x = 0
for Tile in Layer:
floor_roomIDs[r_y][r_x] = floor_map[r_y][r_x]
r_x += 1
r_y += 1
r_y = 0
for Layer in floor_roomIDs:
r_x = 0
for Title in Layer:
if floor_roomIDs[r_y][r_x] == '1':
room_ID = rand_ID.pop(random.randint(0, len(rand_ID)-1))
room_ID_level = globals()['r' + str(room_ID)]
globals()['rc' + str(room_ID)] = room_ID_level.copy()
roomC_ID = 'rc' + str(room_ID)
floor_roomIDs[r_y][r_x] = str(roomC_ID)
if floor_roomIDs[r_y][r_x] == 2:
floor_roomIDs[r_y][r_x] = 'r2'
if floor_roomIDs[r_y][r_x] == '3':
floor_roomIDs[r_y][r_x] = 'r3'
r_x += 1
r_y += 1
def floor_doors():
global floor_map
r_y = 0
for Layer in floor_map:
r_x = 0
for Tile in Layer:
if floor_map[r_y][r_x] == '1' or floor_map[r_y][r_x] == 2:
door_right = False
door_bottom = False
door_left = False
door_top = False
if r_x < 8:
if floor_map[r_y][r_x+1] != '/':
door_right = True
if r_y < 8:
if floor_map[r_y+1][r_x] != '/':
door_bottom = True
if r_x > 0:
if floor_map[r_y][r_x-1] != '/':
door_left = True
if r_y > 0:
if floor_map[r_y-1][r_x] != '/':
door_top = True
if door_top == True:
floor_map[r_y][r_x] = 'a'
if door_left == True:
floor_map[r_y][r_x] = 'b'
if door_bottom == True:
floor_map[r_y][r_x] = 'c'
if door_right == True:
floor_map[r_y][r_x] = 'd'
if door_top == True and door_left == True:
floor_map[r_y][r_x] = 'e'
if door_top == True and door_bottom == True:
floor_map[r_y][r_x] = 'f'
if door_top == True and door_right == True:
floor_map[r_y][r_x] = 'g'
if door_left == True and door_bottom == True:
floor_map[r_y][r_x] = 'h'
if door_left == True and door_right == True:
floor_map[r_y][r_x] = 'i'
if door_bottom == True and door_right == True:
floor_map[r_y][r_x] = 'j'
if door_top == True and door_left == True and door_right == True:
floor_map[r_y][r_x] = 'k'
if door_top == True and door_bottom == True and door_right == True:
floor_map[r_y][r_x] = 'l'
if door_left == True and door_bottom == True and door_right == True:
floor_map[r_y][r_x] = 'm'
if door_top == True and door_bottom == True and door_left == True:
floor_map[r_y][r_x] = 'n'
if door_top == True and door_bottom == True and door_right == True and door_left == True:
floor_map[r_y][r_x] = 'o'
r_x += 1
r_y += 1
def floor_validate():
global room_amount
room_amount = 0
end_room = 0
r_y = 0
for Layer in floor_map:
r_x = 0
for Tile in Layer:
if floor_map[r_y][r_x] == '1':
room_amount += 1
if r_y == 0 or r_y == 1 or r_y == 8 or r_y == 7 or r_x == 0 or r_x == 1 or r_x == 8 or r_x == 7:
if floor_map[r_y][r_x] == '1':
if end_room == 0:
if r_y == 8 and r_x != 8 and r_x != 0:
if floor_map[r_y-1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x+1] == '1':
end_room += 1
if floor_map[r_y][r_x-1] == '1':
end_room += 1
if r_y == 0 and r_x != 8 and r_x != 0:
if floor_map[r_y+1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x+1] == '1':
end_room += 1
if floor_map[r_y][r_x-1] == '1':
end_room += 1
if r_x == 0 and r_y != 8 and r_y != 0:
if floor_map[r_y+1][r_x] == '1':
end_room += 1
if floor_map[r_y-1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x+1] == '1':
end_room += 1
if r_x == 8 and r_y != 8 and r_y != 0:
if floor_map[r_y+1][r_x] == '1':
end_room += 1
if floor_map[r_y-1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x-1] == '1':
end_room += 1
if r_y == 0 and r_x == 0:
if floor_map[r_y+1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x+1] == '1':
end_room += 1
if r_y == 0 and r_x == 8:
if floor_map[r_y+1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x-1] == '1':
end_room += 1
if r_y == 8 and r_x == 0:
if floor_map[r_y-1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x+1] == '1':
end_room += 1
if r_y == 8 and r_x == 8:
if floor_map[r_y-1][r_x] == '1':
end_room += 1
if floor_map[r_y][r_x-1] == '1':
end_room += 1
if end_room > 1:
end_room = 0
if end_room == 1:
floor_map[r_y][r_x] = 2
r_x += 1
r_y += 1
if room_amount < 18 or room_amount > 22 or end_room != 1:
make_floor_outline()
else:
floor_room_IDs()
floor_doors()
def make_floor_outline():
global floor_map
global floor_roomIDs
read = [['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/']]
floor_map = [['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','3','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/']]
floor_roomIDs = [['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','3','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/'],
['/','/','/','/','/','/','/','/','/']]
for i in range(0, 17):
r_y = 0
for Layer in floor_map:
r_x = 0
for Tile in Layer:
if Tile == '3':
floor_map[r_y][r_x+1] = '1'
floor_map[r_y][r_x-1] = '1'
floor_map[r_y+1][r_x] = '1'
floor_map[r_y-1][r_x] = '1'
if Tile == '1':
if read[r_y][r_x] == '/':
read[r_y][r_x] = '1'
if r_y < 8:
if floor_map[r_y+1][r_x] == '/':
random_roomcount = random.randint(0,3)
if random_roomcount == 1:
floor_map[r_y+1][r_x] = '1'
if r_y > 0:
if floor_map[r_y-1][r_x] == '/':
random_roomcount = random.randint(0,3)
if random_roomcount == 1:
floor_map[r_y-1][r_x] = '1'
if r_x < 8:
if floor_map[r_y][r_x+1] == '/':
random_roomcount = random.randint(0,3)
if random_roomcount == 1:
floor_map[r_y][r_x+1] = '1'
if r_x > 0:
if floor_map[r_y][r_x-1] == '/':
random_roomcount = random.randint(0,3)
if random_roomcount == 1:
floor_map[r_y][r_x-1] = '1'
r_x += 1
r_y += 1
floor_validate()
每当我想制作新地板时,我都会调用它。我的房间是数组,r1 = [..] r2 = [..] 等。我尝试做的是复制其中一个数组并将其用作我的房间,这样我就不会更改原始数组。但是,当我到下一层,再次调用该函数并制作数组的副本时,它们与原始数组不同,而是与我第一次调用该函数时复制的数组相同。为什么是这样?
解决方案
在 python 中使用=
永远不会复制任何东西。绝不。
要制作副本,最好使用copy
python 中的模块。您可以在 python 中制作对象的浅拷贝和深拷贝。
deepcopy
如果您list
包含(引用)其他列表或其他对象,则应该使用。
>>> import copy
>>> l = [1, 2, 3]
>>> lc = copy.deepcopy(l)
>>> lc
[1, 2, 3]
>>> lc.append(4)
>>> l
[1, 2, 3]
>>> lc
[1, 2, 3, 4]
推荐阅读
- google-cloud-build - Google CloudBuild 工件 YAML
- html - HAML .each 具有列表和重复项的函数
- javascript - 对象的对象的访问属性
- c# - 合并位时,在移位期间包含 OR 会发生什么
- google-bigquery - 是否有 isnumeric 的 BigQuery 版本
- sequelize.js - 如何在 Sequelize 表中指定范围?
- angular - 更改租户时重新加载整个页面的原因是什么?
- oracle - 适用于 Visual Studio 2017 的 Oracle 开发人员工具 (ODT)
- git - Git:从目录中删除文件,但确保该文件的版本保留在 git 历史记录中
- javascript - Tempusdominus datetimepicker 打开方向