当前位置:网站首页>实例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多维),原列表都不变。
浅拷贝,拷贝的程度浅,只拷贝原数据的首地址,然后通过原数据的首地址,去获取内容。
浅拷贝,对于多维列表,只是第一维深拷贝了
"""
边栏推荐
- Embedded composition and route
- Factors affecting the quality of slip rings in production
- More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
- Void* C is a carrier for realizing polymorphism
- C WinForm [display real-time time in the status bar] - practical exercise 1
- Nb-iot technical summary
- The firmware of the connected j-link does not support the following memory access
- Synchronization of QT multithreading
- Explain task scheduling based on Cortex-M3 in detail (Part 2)
- Charge pump boost principle - this article will give you a simple understanding
猜你喜欢
Various types of questions judged by prime numbers within 100 (C language)
Briefly talk about the identification protocol of mobile port -bc1.2
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
Talk about the function of magnetic beads in circuits
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
C # joint configuration with Halcon
Summary of SIM card circuit knowledge
UE像素流,来颗“减肥药”吧!
Management and use of DokuWiki
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
随机推荐
Why is 1900 not a leap year
Circleq of linked list
Negative pressure generation of buck-boost circuit
Naming rules for FreeRTOS
Solutions to compilation warnings in Quartus II
Use indent to format code
Some tips for using source insight (solve the problem of selecting all)
MySQL之MHA高可用集群
Semiconductor devices (I) PN junction
C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
How to select conductive slip ring
亿学学堂给的证券账户安不安全?哪里可以开户
Network port usage
Drive LED -- GPIO control
leetcode - 445. 两数相加 II
Weidongshan Internet of things learning lesson 1
General makefile (I) single C language compilation template
Fundamentals of C language
Slist of linked list