• <bdo id='9k9l2'></bdo><ul id='9k9l2'></ul>
      <tfoot id='9k9l2'></tfoot>

      <small id='9k9l2'></small><noframes id='9k9l2'>

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

      1. <legend id='9k9l2'><style id='9k9l2'><dir id='9k9l2'><q id='9k9l2'></q></dir></style></legend>

        在循环中每次迭代开始时将变量重新分配给原始值(在循环之前定义)

        Reassign variable to original value (defined prior to the loop) at start of each iteration in loop(在循环中每次迭代开始时将变量重新分配给原始值(在循环之前定义))
          <tbody id='Ofa4A'></tbody>
        <tfoot id='Ofa4A'></tfoot>

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

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

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

                  本文介绍了在循环中每次迭代开始时将变量重新分配给原始值(在循环之前定义)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Python 中,在每次迭代开始时将变量重新分配给原始值(在循环之前定义)时,您会使用 [:].也就是说:

                  In Python, you use [:] when reassigning a variable to an original value (defined prior to the loop) at the start of each iteration. That is to say:

                  original_1D = ['o', 'o', 'o']
                  for i in range(0,3):
                    new = original_1D[:]       # revert back to 'original_1D' list defined before loop
                    new[i] = 'X'
                    print new
                  

                  产生期望和预期的输出:

                  produces output that is both desired and expected:

                  ['X', 'o', 'o']
                  ['o', 'X', 'o']
                  ['o', 'o', 'X']
                  

                  如果原始列表是多维的(original_2D),就会出现我的问题.例如:

                  My issue arises if the original list is multi-dimensional (original_2D). For example:

                  original_2D = [['o', 'o', 'o'],['o', 'o', 'o']]
                  for i in range(0,3):
                    new = original_2D[:]       # revert back to 'original_2D' list defined before loop
                    new[0][i] = 'X'
                    print new
                  

                  由此,我想要以下输出:

                  From this, I want the following output:

                  # Desired
                  [['X', 'o', 'o'], ['o', 'o', 'o']]
                  [['o', 'X', 'o'], ['o', 'o', 'o']]
                  [['o', 'o', 'X'], ['o', 'o', 'o']]
                  

                  但是没有!我明白了:

                  # Actual
                  [['X', 'o', 'o'], ['o', 'o', 'o']]
                  [['X', 'X', 'o'], ['o', 'o', 'o']]
                  [['X', 'X', 'X'], ['o', 'o', 'o']]
                  

                  好像 original_2D 列表每次迭代都会被覆盖.

                  as if the original_2D list gets overwritten which each iteration.

                  我做错了什么?

                  推荐答案

                  在 Python 中,将变量重新分配给原始值时使用 [:]

                  不,您使用它来创建 浅拷贝).html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange" rel="nofollow">序列.如果原始值是不可变序列(例如字符串和元组),则没有必要,如果原始值不是序列,则它将不起作用.

                  No, you use it to create a full-length slice (i.e. shallow copy) of a sequence. If the original value is an immutable sequence (e.g. strings and tuples) it's unnecessary, and if the original value isn't a sequence it won't work.

                  请注意,我在上面强调了浅拷贝 - 切片创建的新对象包含对与原始对象相同的对象的引用.如果您的原始序列包含对可变对象的引用(例如,列表),这可能是一个问题.

                  Note that I have emphasised shallow copy above - the new object created by the slice contains references to the same objects as the original. If your original sequence contains references to mutable objects (like, for example, lists), this can be a problem.

                  要么使用 copy.deepcopy 创建一个深(而不是浅)副本:

                  Either use copy.deepcopy to create a deep (rather than shallow) copy:

                  from copy import deepcopy
                  
                  new = deepcopy(original2D)
                  

                  或显式创建子列表的浅表副本,例如使用一个列表理解:

                  or explicitly create shallow copies of the sub-lists too, using e.g. a list comprehension:

                  new = [row[:] for row in original2D]
                  

                  前者更容易扩展到更高的维度.

                  The former scales more easily to higher dimensions.

                  这篇关于在循环中每次迭代开始时将变量重新分配给原始值(在循环之前定义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

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

                      <legend id='6qHuW'><style id='6qHuW'><dir id='6qHuW'><q id='6qHuW'></q></dir></style></legend>
                        1. <tfoot id='6qHuW'></tfoot>
                            <bdo id='6qHuW'></bdo><ul id='6qHuW'></ul>
                          • <small id='6qHuW'></small><noframes id='6qHuW'>