Skip to Content
Python数据处理
book

Python数据处理

by Jacqueline Kazil, Katharine Jarmul
July 2017
Intermediate to advanced
398 pages
11h 54m
Chinese
Posts & Telecom Press
Content preview from Python数据处理
364
附录
E
这里,我们的目的不是让你对
Python
中的内存分配有深入的理解,而是意识到我们可能不
会总是去思考我们到底赋值了什么。在处理列表和字典时,我们需要知道和理解的是,在
我们将它赋值为一个新变量的时候,新的变量和旧的变量仍然是内存中的
相同对象
。如果
我们改变其中一个,也改变了另一个。如果只想要改变其中一个或另一个,或者需要创建
一个新对象作为对象的副本,需要使用
copy
方法。
让我们通过最后一个示例来解释
copy
与赋值:
In [21]: a = {}
In [22]: id(a)
Out[22]: 140491293143120
In [23]: b = a
In [24]: id(b)
Out[24]: 140491293143120
In [25]: a['test'] = 1
In [26]: b
Out[26]: {'test': 1}
In [27]: c = b.copy()
In [28]: id(c)
Out[28]: 140491293140144
In [29]: c['test_2'] = 2
In [30]: c
Out[30]: {'test': 1, 'test_2': 2}
In [31]: b
Out[31]: {'test': 1}
在这行代码中,我们看到,当我们修改
a
时,同样修改了
b
,因为它们存储在内存中相
同的位置。
使用
copy
方法我们创建了一个新的变量,
c
,这是第一个字典的副本。
这行代码中,我们看到 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据科学中的实用统计学(第2版)

数据科学中的实用统计学(第2版)

Peter Bruce, Andrew Bruce, Peter Gedeck
Java持续交付

Java持续交付

Daniel Bryant, Abraham Marín-Pérez
解密金融数据

解密金融数据

Justin Pauley

Publisher Resources

ISBN: 9787115459190