<bdo id='0Q1JV'></bdo><ul id='0Q1JV'></ul>

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

      <tfoot id='0Q1JV'></tfoot>
      <legend id='0Q1JV'><style id='0Q1JV'><dir id='0Q1JV'><q id='0Q1JV'></q></dir></style></legend>
      1. <small id='0Q1JV'></small><noframes id='0Q1JV'>

        切片列表时,Python 是否复制对对象的引用?

        时间:2023-08-31
        <tfoot id='naVwl'></tfoot><i id='naVwl'><tr id='naVwl'><dt id='naVwl'><q id='naVwl'><span id='naVwl'><b id='naVwl'><form id='naVwl'><ins id='naVwl'></ins><ul id='naVwl'></ul><sub id='naVwl'></sub></form><legend id='naVwl'></legend><bdo id='naVwl'><pre id='naVwl'><center id='naVwl'></center></pre></bdo></b><th id='naVwl'></th></span></q></dt></tr></i><div id='naVwl'><tfoot id='naVwl'></tfoot><dl id='naVwl'><fieldset id='naVwl'></fieldset></dl></div><legend id='naVwl'><style id='naVwl'><dir id='naVwl'><q id='naVwl'></q></dir></style></legend>
        本文介绍了切片列表时,Python 是否复制对对象的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

        问题描述

        当一个列表被切片时,对其内容的引用是从原始列表中复制的吗?我可以想象这可能没有必要,但我读到了相反的内容(顺便提及).

        When a list is sliced, are the references to its contents copied from the original list? I can imagine that this may not be necessary, but I read the opposite (mentioned in passing).

        对于很长的my_list,这个问题例如对于以下成语很重要:

        This question matters for instance for the following idiom, in the case of a very long my_list:

        for (first_elmt, second_elmt) in itertools.izip(my_list[:-1], my_list[1:]):
            …
        

        副本会占用内存,并且可能会占用一些时间.我比较了用 xrange() 在 1 亿个整数列表上循环 first_elmt 的索引.切片方法实际上快了 20%,但似乎确实复制了引用(系统时间更长).真的是这样吗?

        A copy would use up both memory and, presumably, some time. I compared to looping over the index of first_elmt with xrange(), on a list of 100 million integers. The slicing approach is actually 20% faster, but does seem to copy references (the system time is longer). Is this indeed the case?

        PS:我现在意识到切片复制引用是很自然的:如果原始列表被修改,切片不会改变,所以更容易实现切片复制原始列表的引用.不过,指向 CPython 实现的指针会很有趣.

        PS: I now realize that it is quite natural that slices copy the references: if the original list is modified, the slice does not change, so it is easier to have the implementation of the slice copy the references of the original list. A pointer to the CPython implementation would be interesting, though.

        推荐答案

        切片会复制引用.如果您有一个包含 1 亿个事物的列表:

        Slicing will copy the references. If you have a list of 100 million things:

        l = [object() for i in xrange(100000000)]
        

        然后你切一片:

        l2 = l[:-1]
        

        l2 将拥有自己的 99,999,999 个指针的后备数组,而不是共享 l 的数组.但是,这些指针引用的对象不会被复制:

        l2 will have its own backing array of 99,999,999 pointers, rather than sharing l's array. However, the objects those pointers refer to are not copied:

        >>> l2[0] is l[0]
        True
        

        如果您想迭代列表的重叠元素对而不进行复制,您可以使用已前进一位的迭代器zip列表:

        If you want to iterate over overlapping pairs of elements of a list without making a copy, you can zip the list with an iterator that has been advanced one position:

        second_items = iter(l)
        next(second_items, None) # Avoid exception on empty input
        for thing1, thing2 in itertools.izip(l, second_items):
            whatever()
        

        这利用了 zip 在任何输入迭代器停止时停止这一事实.这可以扩展到您已经使用 itertools 使用迭代器的情况.三通

        This takes advantage of the fact that zip stops when any input iterator stops. This can be extended to cases where you're already working with an iterator using itertools.tee

        i1, i2 = itertools.tee(iterator)
        next(i2, None)
        for thing1, thing2 in itertools.izip(i1, i2):
            whatever()
        

        这篇关于切片列表时,Python 是否复制对对象的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

        上一篇:Python:deepcopy(list) vs new_list = old_list[:] 下一篇:如何将文件复制到 Python 脚本中的特定文件夹?

        相关文章

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

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

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