Python 迭代字典以在另一个列表中创建项目列表
我有一本字典叫《舰队几艘船》。字典中的每艘船长度超过1(至少由两组坐标组成,例如Python 迭代字典以在另一个列表中创建项目列表,python,python-3.x,list,dictionary,for-loop,Python,Python 3.x,List,Dictionary,For Loop,我有一本字典叫《舰队几艘船》。字典中的每艘船长度超过1(至少由两组坐标组成,例如[[3,1],[3,2]]和[[2,3],[2,4],[2,5] 使用下面的代码,我成功地获得了所有船只的坐标,但不知何故,我不知道如何制作舰队列表,其中每个元素都是带有坐标的列表。对于上面的示例,我需要这样的结果: [[[3, 1], [3, 2]], [[2, 3], [2, 4], [2, 5]]] 我想需要另一个for循环,但我无法将其正确集成 有问题的代码位于函数位置_ship() 对不起,没有问清楚
[[3,1],[3,2]]
和[[2,3],[2,4],[2,5]
使用下面的代码,我成功地获得了所有船只的坐标,但不知何故,我不知道如何制作舰队列表,其中每个元素都是带有坐标的列表。对于上面的示例,我需要这样的结果:
[[[3, 1], [3, 2]],
[[2, 3], [2, 4], [2, 5]]]
我想需要另一个for
循环,但我无法将其正确集成
有问题的代码位于函数位置_ship()
对不起,没有问清楚 新的71号线和78号线的就位功能解决了我的问题
from random import randint
num_cols = 5 # board size direction x
num_rows = 4 # board size direction y
empty_field = "~"
cheat_mode = 1 # 1 = Yes; 0 = No
line1 = " "
line2 = "------"
# Type and quantity of ships in fleet
fleet = {
"Aircraft Carrier": [5, "A"],
"Battleship": [4, "B"],
"Destroyer": [3, "D"],
"Submarine": [3, "S"],
"Patrol Boat": [2, "P"]
}
all_ship_parts = []
for ship in fleet:
all_ship_parts1 = fleet[ship][0]
all_ship_parts.append(all_ship_parts1)
all_ship_parts = sum(all_ship_parts)
num_of_turns = all_ship_parts + 1
board = []
# Create a blank board.
for each_row in range(num_rows):
board_col = []
for each_col in range(num_cols):
board_col.append(empty_field)
board.append(board_col)
def print_board():
print(" ", end="")
for i in range(0, num_cols):
print(" " + str(i + 1) + " ", end=' ')
print(" ", end=' ')
print("")
print(line1 + line2 * num_cols)
for i in range(0, num_rows):
if i < 9:
print(str(i + 1) + " | ", end=' ')
else:
print(str(i + 1) + "| ", end=' ')
for j in range(0, num_cols):
if board[i][j] == empty_field:
print(" ", end=' ')
else:
print(board[i][j], end=' ')
print(" | ", end=' ')
print("")
print(line1 + line2 * num_cols)
# print_board()
total = []
def place_ship():
# place ship based on orientation
list_ship_coordinates = []
if ori == "v":
for i in range(fleet[ship][0]):
board[x + i][y] = fleet[ship][1]
part_of_ship = [x + i + 1, y + 1]
list_ship_coordinates.append(part_of_ship)
total.append(list_ship_coordinates)
print(list_ship_coordinates)
elif ori == "h":
for i in range(fleet[ship][0]):
board[x][y + i] = fleet[ship][1]
part_of_ship = [x + 1, y + i + 1]
list_ship_coordinates.append(part_of_ship)
total.append(list_ship_coordinates)
print(list_ship_coordinates)
return board
def validate():
if ori == "v" and x + fleet[ship][0] > num_rows:
return False
elif ori == "h" and y + fleet[ship][0] > num_cols:
return False
else:
if ori == "v":
for i in range(fleet[ship][0]):
if board[x + i][y] != empty_field:
return False
elif ori == "h":
for i in range(fleet[ship][0]):
if board[x][y + i] != empty_field:
return False
return True
for ship in fleet:
valid = False
while not valid:
x = randint(0, num_rows - 1)
y = randint(0, num_cols - 1)
o = randint(0, 1)
if o == 0:
ori = "v"
else:
ori = "h"
valid = validate()
board = place_ship()
print("-----------")
print(total)
print("-----------")
print(len(total))
print(sum(len(x) for x in total))
print("-----------")
print_board()
# print(board[0][2])
# print(board[1][2])
# print(board[2][2])
# print(board[3][2])
来自随机导入randint
num_cols=5#电路板尺寸方向x
行数=4#板尺寸方向y
空_字段=“~”
作弊模式=1#1=是;0=否
line1=“”
第2行=“----”
#船队中船舶的类型和数量
车队={
“航空母舰”:[5,“A”],
“战舰”:[4,“B”],
“驱逐舰”:[3,“D”],
“潜艇”:[3,“S”],
“巡逻艇”:[2,“P”]
}
所有装运零件=[]
对于船队中的船舶:
所有船舶零件1=船队[船舶][0]
所有装运零件。附加(所有装运零件1)
所有装运零件=总和(所有装运零件)
转动次数=所有装运零件+1
董事会=[]
#创建一个空白板。
对于范围中的每一行(num行):
board_col=[]
对于范围内的每个列(num列):
板列追加(空字段)
board.append(board\u col)
def打印板():
打印(“,end=”“)
对于范围内的i(0,num_cols):
打印(“+str(i+1)+”,结束=”)
打印(“”,结束=“”)
打印(“”)
打印(第1行+第2行*数量)
对于范围内的i(0,num_行):
如果i<9:
打印(str(i+1)+“|”,end=”)
其他:
打印(str(i+1)+“|”,end=”)
对于范围内的j(0,num_cols):
如果板[i][j]==空_字段:
打印(“”,结束=“”)
其他:
打印(板[i][j],结束=“”)
打印(“|”,结束=”)
打印(“”)
打印(第1行+第2行*数量)
#印刷电路板()
总计=[]
def place_ship():
#根据方向放置船舶
列出船舶坐标=[]
如果ori==“v”:
对于射程内的i(舰队[ship][0]):
董事会[x+i][y]=船队[船舶][1]
船舶部分=[x+i+1,y+1]
列出船舶坐标。追加(船舶的部分)
总计。追加(列出船舶坐标)
打印(列出船舶坐标)
elif ori==“h”:
对于射程内的i(舰队[ship][0]):
董事会[x][y+i]=船队[船舶][1]
船舶的部分=[x+1,y+i+1]
列出船舶坐标。追加(船舶的部分)
总计。追加(列出船舶坐标)
打印(列出船舶坐标)
返回板
def validate():
如果ori==“v”和x+舰队[ship][0]>行数:
返回错误
elif ori==“h”和y+船队[船舶][0]>数量:
返回错误
其他:
如果ori==“v”:
对于射程内的i(舰队[ship][0]):
如果板[x+i][y]!=空字段:
返回错误
elif ori==“h”:
对于射程内的i(舰队[ship][0]):
如果板[x][y+i]!=空字段:
返回错误
返回真值
对于船队中的船舶:
有效=错误
虽然无效:
x=randint(0,num_行-1)
y=randint(0,num\u cols-1)
o=randint(0,1)
如果o==0:
ori=“v”
其他:
ori=“h”
valid=validate()
board=地点和船()
打印(------------)
打印(总计)
打印(------------)
打印(总页数)
打印(总计x的长度(x))
打印(------------)
印刷电路板()
#打印(电路板[0][2])
#打印(电路板[1][2])
#打印(电路板[2][2])
#打印(电路板[3][2])
欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南,正如您创建此帐户时所建议的。适用于此处。在您发布MCVE代码并准确描述问题之前,我们无法有效帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现问题你描述的lem。StackOverflow不是一个编码或教程资源。我现在已经把全部代码放进去了,希望现在能更容易理解。请再次阅读并遵循发布指南。此代码不会执行,并且不是你所说的问题的最小值。
from random import randint
num_cols = 5 # board size direction x
num_rows = 4 # board size direction y
empty_field = "~"
cheat_mode = 1 # 1 = Yes; 0 = No
line1 = " "
line2 = "------"
# Type and quantity of ships in fleet
fleet = {
"Aircraft Carrier": [5, "A"],
"Battleship": [4, "B"],
"Destroyer": [3, "D"],
"Submarine": [3, "S"],
"Patrol Boat": [2, "P"]
}
all_ship_parts = []
for ship in fleet:
all_ship_parts1 = fleet[ship][0]
all_ship_parts.append(all_ship_parts1)
all_ship_parts = sum(all_ship_parts)
num_of_turns = all_ship_parts + 1
board = []
# Create a blank board.
for each_row in range(num_rows):
board_col = []
for each_col in range(num_cols):
board_col.append(empty_field)
board.append(board_col)
def print_board():
print(" ", end="")
for i in range(0, num_cols):
print(" " + str(i + 1) + " ", end=' ')
print(" ", end=' ')
print("")
print(line1 + line2 * num_cols)
for i in range(0, num_rows):
if i < 9:
print(str(i + 1) + " | ", end=' ')
else:
print(str(i + 1) + "| ", end=' ')
for j in range(0, num_cols):
if board[i][j] == empty_field:
print(" ", end=' ')
else:
print(board[i][j], end=' ')
print(" | ", end=' ')
print("")
print(line1 + line2 * num_cols)
# print_board()
total = []
def place_ship():
# place ship based on orientation
list_ship_coordinates = []
if ori == "v":
for i in range(fleet[ship][0]):
board[x + i][y] = fleet[ship][1]
part_of_ship = [x + i + 1, y + 1]
list_ship_coordinates.append(part_of_ship)
total.append(list_ship_coordinates)
print(list_ship_coordinates)
elif ori == "h":
for i in range(fleet[ship][0]):
board[x][y + i] = fleet[ship][1]
part_of_ship = [x + 1, y + i + 1]
list_ship_coordinates.append(part_of_ship)
total.append(list_ship_coordinates)
print(list_ship_coordinates)
return board
def validate():
if ori == "v" and x + fleet[ship][0] > num_rows:
return False
elif ori == "h" and y + fleet[ship][0] > num_cols:
return False
else:
if ori == "v":
for i in range(fleet[ship][0]):
if board[x + i][y] != empty_field:
return False
elif ori == "h":
for i in range(fleet[ship][0]):
if board[x][y + i] != empty_field:
return False
return True
for ship in fleet:
valid = False
while not valid:
x = randint(0, num_rows - 1)
y = randint(0, num_cols - 1)
o = randint(0, 1)
if o == 0:
ori = "v"
else:
ori = "h"
valid = validate()
board = place_ship()
print("-----------")
print(total)
print("-----------")
print(len(total))
print(sum(len(x) for x in total))
print("-----------")
print_board()
# print(board[0][2])
# print(board[1][2])
# print(board[2][2])
# print(board[3][2])