Python:deepcopy(list) vs new_list = old_list[:]

2023-08-31Python开发问题
3

本文介绍了Python:deepcopy(list) vs new_list = old_list[:]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在做练习 #9,来自 http://openbookproject.net/thinkcs/python/english2e/ch09.html 并且遇到了一些没有意义的东西.

I'm doing exercise #9 from http://openbookproject.net/thinkcs/python/english2e/ch09.html and have ran into something that doesn't make sense.

该练习建议使用 copy.deepcopy() 使我的任务更轻松,但我不明白它是如何做到的.

The exercise suggests using copy.deepcopy() to make my task easier but I don't see how it could.

def add_row(matrix):
    """
        >>> m = [[0, 0], [0, 0]]
        >>> add_row(m)
        [[0, 0], [0, 0], [0, 0]]
        >>> n = [[3, 2, 5], [1, 4, 7]]
        >>> add_row(n)
        [[3, 2, 5], [1, 4, 7], [0, 0, 0]]
        >>> n
        [[3, 2, 5], [1, 4, 7]]
    """

    import copy
    # final = copy.deepcopy(matrix)  # first way
    final = matrix[:]                # second way
    li = []
    for i in range(len(matrix[0])):
        li.append(0)
    # return final.append(li)  # why doesn't this work?
    final.append(li)            # but this does
    return final

我很困惑为什么本书建议在简单的 list[:] 复制它时使用 deepcopy().我用错了吗?我的功能完全不正常吗?

I'm confused why the book suggests using deepcopy() when a simple list[:] copies it. Am I using it wrong? Is my function completely out of wack?

我也有一些混乱的返回值.问题是上面代码中的文档.

I also have some confusion returning values. the question is documents in the code above.

TIA

推荐答案

你问了两个问题:

matrix[:] 是一个浅拷贝——它只拷贝直接存储在其中的元素,不会递归地拷贝数组或其他引用的元素自身之内.这意味着:

matrix[:] is a shallow copy -- it only copies the elements directly stored in it, and doesn't recursively duplicate the elements of arrays or other references within itself. That means:

a = [[4]]
b = a[:]
a[0].append(5)
print b[0] # Outputs [4, 5], as a[0] and b[0] point to the same array

如果您将对象存储在 a 中也会发生同样的情况.

The same would happen if you stored an object in a.

deepcopy() 自然是一个deep copy——它递归地复制每个元素,一直沿树向下:

deepcopy() is, naturally, a deep copy -- it makes copies of each of its elements recursively, all the way down the tree:

a = [[4]]
c = copy.deepcopy(a)
a[0].append(5)
print c[0] # Outputs [4], as c[0] is a copy of the elements of a[0] into a new array

返回

return final.append(li) 与调用 append 并返回 final 不同,因为 list.append 不返回列表对象本身, 它返回 None

Returning

return final.append(li) is different from calling append and returning final because list.append does not return the list object itself, it returns None

这篇关于Python:deepcopy(list) vs new_list = old_list[:]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10