<tfoot id='SECCQ'></tfoot>

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

    1. <small id='SECCQ'></small><noframes id='SECCQ'>

        这段代码中 list[:] 的含义是什么?

        时间:2023-10-19
        <tfoot id='VOE0F'></tfoot>

          <tbody id='VOE0F'></tbody>

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

          1. <legend id='VOE0F'><style id='VOE0F'><dir id='VOE0F'><q id='VOE0F'></q></dir></style></legend>
            • <bdo id='VOE0F'></bdo><ul id='VOE0F'></ul>

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

                  本文介绍了这段代码中 list[:] 的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  此代码来自 Python 的文档.我有点困惑.

                  This code is from Python's Documentation. I'm a little confused.

                  words = ['cat', 'window', 'defenestrate']
                  for w in words[:]:
                      if len(w) > 6:
                          words.insert(0, w)
                  print(words)
                  

                  以下是我最初的想法:

                  words = ['cat', 'window', 'defenestrate']
                  for w in words:
                      if len(w) > 6:
                          words.insert(0, w)
                  print(words)
                  

                  为什么这段代码创建了一个无限循环而第一个没有?

                  Why does this code create a infinite loop and the first one doesn't?

                  推荐答案

                  这是陷阱之一!python的,可以逃脱初学者.

                  This is one of the gotchas! of python, that can escape beginners.

                  words[:] 是这里的魔法酱.

                  观察:

                  >>> words =  ['cat', 'window', 'defenestrate']
                  >>> words2 = words[:]
                  >>> words2.insert(0, 'hello')
                  >>> words2
                  ['hello', 'cat', 'window', 'defenestrate']
                  >>> words
                  ['cat', 'window', 'defenestrate']
                  

                  现在没有 [:]:

                  >>> words =  ['cat', 'window', 'defenestrate']
                  >>> words2 = words
                  >>> words2.insert(0, 'hello')
                  >>> words2
                  ['hello', 'cat', 'window', 'defenestrate']
                  >>> words
                  ['hello', 'cat', 'window', 'defenestrate']
                  

                  这里要注意的主要是 words[:] 返回现有列表的 copy,因此您正在迭代未修改的副本.

                  The main thing to note here is that words[:] returns a copy of the existing list, so you are iterating over a copy, which is not modified.

                  您可以使用 id() 检查您是否引用了相同的列表:

                  You can check whether you are referring to the same lists using id():

                  第一种情况:

                  >>> words2 = words[:]
                  >>> id(words2)
                  4360026736
                  >>> id(words)
                  4360188992
                  >>> words2 is words
                  False
                  

                  第二种情况:

                  >>> id(words2)
                  4360188992
                  >>> id(words)
                  4360188992
                  >>> words2 is words
                  True
                  

                  值得注意的是,[i:j]被称为切片操作符,它的作用是返回一个从索引<开始的列表的新副本code>i,直到(但不包括)索引 j.

                  It is worth noting that [i:j] is called the slicing operator, and what it does is it returns a fresh copy of the list starting from index i, upto (but not including) index j.

                  所以,words[0:2] 给你

                  >>> words[0:2]
                  ['hello', 'cat']
                  

                  省略起始索引表示默认为0,省略最后一个索引表示默认为len(words),最终结果是收到一个整个列表的副本.

                  Omitting the starting index means it defaults to 0, while omitting the last index means it defaults to len(words), and the end result is that you receive a copy of the entire list.

                  如果你想让你的代码更具可读性,我推荐 copy 模块.

                  If you want to make your code a little more readable, I recommend the copy module.

                  from copy import copy 
                  
                  words = ['cat', 'window', 'defenestrate']
                  for w in copy(words):
                      if len(w) > 6:
                          words.insert(0, w)
                  print(words)
                  

                  这基本上和你的第一个代码片段做同样的事情,并且更具可读性.

                  This basically does the same thing as your first code snippet, and is much more readable.

                  或者(正如 DSM 在评论中提到的)和在 python >=3 上,您也可以使用 words.copy() 来做同样的事情.

                  Alternatively (as mentioned by DSM in the comments) and on python >=3, you may also use words.copy() which does the same thing.

                  这篇关于这段代码中 list[:] 的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python在列表理解中使用枚举 下一篇:如何将空格分隔的键值对字符串转换为字典

                  相关文章

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

                    <tfoot id='6dygl'></tfoot>
                      <bdo id='6dygl'></bdo><ul id='6dygl'></ul>

                    <small id='6dygl'></small><noframes id='6dygl'>