• <legend id='kUf8v'><style id='kUf8v'><dir id='kUf8v'><q id='kUf8v'></q></dir></style></legend>
    <tfoot id='kUf8v'></tfoot>

    <small id='kUf8v'></small><noframes id='kUf8v'>

      <bdo id='kUf8v'></bdo><ul id='kUf8v'></ul>
      <i id='kUf8v'><tr id='kUf8v'><dt id='kUf8v'><q id='kUf8v'><span id='kUf8v'><b id='kUf8v'><form id='kUf8v'><ins id='kUf8v'></ins><ul id='kUf8v'></ul><sub id='kUf8v'></sub></form><legend id='kUf8v'></legend><bdo id='kUf8v'><pre id='kUf8v'><center id='kUf8v'></center></pre></bdo></b><th id='kUf8v'></th></span></q></dt></tr></i><div id='kUf8v'><tfoot id='kUf8v'></tfoot><dl id='kUf8v'><fieldset id='kUf8v'></fieldset></dl></div>

        为战舰创建和初始化 5x5 网格

        Create and initialize 5x5 grid for Battleships(为战舰创建和初始化 5x5 网格)

              <small id='xtilc'></small><noframes id='xtilc'>

                <bdo id='xtilc'></bdo><ul id='xtilc'></ul>
                <i id='xtilc'><tr id='xtilc'><dt id='xtilc'><q id='xtilc'><span id='xtilc'><b id='xtilc'><form id='xtilc'><ins id='xtilc'></ins><ul id='xtilc'></ul><sub id='xtilc'></sub></form><legend id='xtilc'></legend><bdo id='xtilc'><pre id='xtilc'><center id='xtilc'></center></pre></bdo></b><th id='xtilc'></th></span></q></dt></tr></i><div id='xtilc'><tfoot id='xtilc'></tfoot><dl id='xtilc'><fieldset id='xtilc'></fieldset></dl></div>

                <legend id='xtilc'><style id='xtilc'><dir id='xtilc'><q id='xtilc'></q></dir></style></legend><tfoot id='xtilc'></tfoot>
                    <tbody id='xtilc'></tbody>
                  本文介绍了为战舰创建和初始化 5x5 网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我刚刚完成了 CodeAcademy Battleship 问题的一部分,并提交了正确的答案,但我无法理解为什么它是正确的.

                  So I just completed a section of CodeAcademy Battleship problem, and have submitted a correct answer but am having trouble understanding why it is correct.

                  这个想法是建立一个 5x5 的网格作为板,用O"填充.我使用的正确代码是:

                  The idea is to build a 5x5 grid as a board, filled with "O's". The correct code I used was:

                  board = []
                  board_size=5
                  
                  for i in range(board_size):
                  
                      board.append(["O"] *5)
                  

                  但是我很困惑为什么这没有在一行中创建 25 个O",因为我从未指定迭代到单独的行.我试过了

                  However I'm confused as to why this didn't create 25 "O's" in one single row as I never specified to iterate to a separate row. I tried

                  for i in range(board_size):
                  
                      board[i].append(["O"] *5)
                  

                  但这给了我错误:IndexError: list index out of range.谁能解释一下为什么第一个是正确的而不是第二个?

                  but this gave me the error: IndexError: list index out of range. Can anyone explain why the first one is correct and not the second one?

                  推荐答案

                  ["O"]*5
                  

                  这将创建一个大小为 5 的列表,用O"填充:["O", "O", "O", "O", "O"]

                  This creates a list of size 5, filled with "O": ["O", "O", "O", "O", "O"]

                  board.append(["O"] *5)
                  

                  这会将上述列表附加(添加到列表末尾)到 board[].循环执行 5 次会创建一个包含上述 5 个列表的列表.

                  This appends (adds to the end of the list) the above list to board[]. Doing this 5 times in a loop creates a list filled with 5 of the above lists.

                  [["O", "O", "O", "O", "O"],
                  ["O", "O", "O", "O", "O"],
                  ["O", "O", "O", "O", "O"],
                  ["O", "O", "O", "O", "O"],
                  ["O", "O", "O", "O", "O"]]
                  

                  您的代码不起作用,因为列表没有在 python 中使用大小进行初始化,它只是从一个空容器 [] 开始.为了使你的工作,你可以这样做:

                  Your code did not work, because lists are not initialized with a size in python, it just starts as an empty container []. To make yours work, you could have done:

                  board = [[],[],[],[],[]]
                  

                  在你的循环中:

                  board[i] = ["O"]*5
                  

                  这篇关于为战舰创建和初始化 5x5 网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                      <legend id='pRLiC'><style id='pRLiC'><dir id='pRLiC'><q id='pRLiC'></q></dir></style></legend>

                        <tfoot id='pRLiC'></tfoot>

                          <tbody id='pRLiC'></tbody>

                      1. <i id='pRLiC'><tr id='pRLiC'><dt id='pRLiC'><q id='pRLiC'><span id='pRLiC'><b id='pRLiC'><form id='pRLiC'><ins id='pRLiC'></ins><ul id='pRLiC'></ul><sub id='pRLiC'></sub></form><legend id='pRLiC'></legend><bdo id='pRLiC'><pre id='pRLiC'><center id='pRLiC'></center></pre></bdo></b><th id='pRLiC'></th></span></q></dt></tr></i><div id='pRLiC'><tfoot id='pRLiC'></tfoot><dl id='pRLiC'><fieldset id='pRLiC'></fieldset></dl></div>
                      2. <small id='pRLiC'></small><noframes id='pRLiC'>

                            <bdo id='pRLiC'></bdo><ul id='pRLiC'></ul>