当前位置:网站首页>tuple and point
tuple and point
2022-07-05 02:24:00 【Incoming brother】
Tuples
About tuples : Tuples are in Python of use ()
Express , Unlike lists, tuples cannot be modified
If the tuple has only one value , We can't just write a value , This will turn Python The interpreter is confused
We're going to write ( value ,)
Form like this
The purpose of tuples is to tell people who read code , You don't want to change this thing
hello = [1,2,3]
hello[1] = 4
# Wrong writing
tuple() and list()
We can convert tuple type to list type , You can also convert a list type to a tuple type
a = tuple([1,2,4])
print(type(a))
# Output is tuple type
a = list((1,2,3,4))
print(tupe(a))
# Output is list type
About quoting
Before you know about references , Let's compare the two programs first
a = 1
b = a
b = 2
print(a) #1
print(b) #2
We found this a The value of
a = [1,2,3]
b = a
b[0] = 0
print(a) # [0,2,3]
print(b) # [0,2,3]
We found out here a[1] The value of has changed , Mainly because of a It's a reference ,a Point to the list
b = a, It means to make b Also points to the list
Pass on references
def add(p):
a.append(4)
a = [1,2,3]
add(p)
print(a)
# [1,2,3,4]
This happens to lists and dictionaries
If you don't want this quote to happen , You can use full assignment
import copy
a = [1,2,3,4]
b = copy.copy(a)
b[1] = 0
print(a) #[1,2,3,4]
print(b) #[0,2,3,4]
print('========')
# If a list is nested in the list
c = [1,[1,2,3]]
d = copy.deepcopy(c)
d[1] = 0
print(c)
print(d)
summary :
1). The list is variable , Tuples are immutable
2). The assignment of a list is a reference
3). The complete assignment is copy.copy(list),copy.deepcopy(list)
边栏推荐
- A tab Sina navigation bar
- 【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
- CAM Pytorch
- One click generation and conversion of markdown directory to word format
- openresty ngx_lua执行阶段
- Android advanced interview question record in 2022
- runc hang 导致 Kubernetes 节点 NotReady
- Vulnstack3
- openresty ngx_ Lua variable operation
- The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
猜你喜欢
Go RPC call
Bert fine tuning skills experiment
Win: use shadow mode to view the Desktop Session of a remote user
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
openresty ngx_lua执行阶段
如何搭建一支搞垮公司的技术团队?
【LeetCode】501. Mode in binary search tree (2 wrong questions)
如何做一个炫酷的墨水屏电子钟?
官宣!第三届云原生编程挑战赛正式启动!
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
随机推荐
Process scheduling and termination
Android advanced interview question record in 2022
Asynchronous and promise
官宣!第三届云原生编程挑战赛正式启动!
openresty ngx_lua执行阶段
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
数据库和充值都没有了
Pytorch common code snippet collection
Using druid to connect to MySQL database reports the wrong type
问题解决:AttributeError: ‘NoneType‘ object has no attribute ‘append‘
Win: add general users to the local admins group
How to build a technical team that will bring down the company?
[understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
Data guard -- theoretical explanation (III)
85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
Missile interception -- UPC winter vacation training match
Practical case of SQL optimization: speed up your database
Valentine's Day flirting with girls to force a small way, one can learn