当前位置:网站首页>实例007:copy 将一个列表的数据复制到另一个列表中。
实例007:copy 将一个列表的数据复制到另一个列表中。
2022-07-05 08:16:00 【懒笑翻】
实例007:copy
题目:将一个列表的数据复制到另一个列表中。
程序分析:
我们可以使用如下方法复制:直接赋值=、索引[:]、copy()、deepcopy()
代码如下:
import copy
a = [1, 2, 3, 4, ['a', 'b']]
b = a # 赋值
c = a[:] # 浅拷贝
d = copy.copy(a) # 浅拷贝
e = copy.deepcopy(a) # 深拷贝
a.append(5)
a[4].append('c')
print('a=', a)
print('b=', b)
print('c=', c)
print('d=', d)
print('e=', e)
a= [1, 2, 3, 4, ['a', 'b', 'c'], 5]
b= [1, 2, 3, 4, ['a', 'b', 'c'], 5]
c= [1, 2, 3, 4, ['a', 'b', 'c']]
d= [1, 2, 3, 4, ['a', 'b', 'c']]
e= [1, 2, 3, 4, ['a', 'b']]
总结:
"""
深拷贝,拷贝的程度深:自己新开辟了一块内存,将被拷贝内容全部拷贝过来了
对列表深拷贝就是无论怎样改动新列表(单维or多维),原列表都不变。
浅拷贝,拷贝的程度浅,只拷贝原数据的首地址,然后通过原数据的首地址,去获取内容。
浅拷贝,对于多维列表,只是第一维深拷贝了
"""
边栏推荐
- Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
- Why is 1900 not a leap year
- Tailq of linked list
- Consul安装
- MHA High available Cluster for MySQL
- Gradle composite construction
- Buildroot system for making raspberry pie cm3
- Network communication process
- Soem EtherCAT source code analysis I (data type definition)
- My-basic application 1: introduction to my-basic parser
猜你喜欢
MySQL之MHA高可用集群
Various types of questions judged by prime numbers within 100 (C language)
Network communication process
Embedded composition and route
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
Carrier period, electrical speed, carrier period variation
UEFI development learning 3 - create UEFI program
1-stm32 operation environment construction
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
Take you to understand the working principle of lithium battery protection board
随机推荐
[paper reading] the latest transfer ability in deep learning: a survey in 2022
Live555 push RTSP audio and video stream summary (I) cross compilation
Management and use of DokuWiki (supplementary)
亿学学堂给的证券账户安不安全?哪里可以开户
Google sitemap files for rails Projects - Google sitemap files for rails projects
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
C language enhancement -- pointer
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
Shell script basic syntax
C WinForm [view status bar -- statusstrip] - Practice 2
Imx6ull bare metal development learning 2- use C language to light LED indicator
动力电池UL2580测试项目包括哪些
Nb-iot technical summary
Summary -st2.0 Hall angle estimation
MySQL MHA high availability cluster
C language # and #
OLED 0.96 inch test
Synchronization of QT multithreading
STM32 single chip microcomputer -- volatile keyword
Network port usage