当前位置:网站首页>The difference between tuples and lists
The difference between tuples and lists
2022-06-21 08:33:00 【Luckly Sister】
Master the difference between tuples and lists
● Master the declaration and assignment of tuples
● Master the relevant operations applicable to tuples
Python Tuples and Python List data similar , It's all linear tables . The only difference is ,Python The stored data after tuple assignment cannot be modified by the program , A tuple can be seen as a list that can only read data but not modify data . Because tuple types and list types have a lot in common , What has been said about list types , No more repetition , Focus on the differences between tuples and lists , Then we discuss the immutability of tuple data .
1、 Tuple declaration and assignment
The syntax of declaring a tuple and assigning values is the same as that of a list , Here's the difference Tuples use braces , Use square brackets for lists , Between elements, there are also Comma separated . It should be noted that , When a tuple has only one element , You need to add an English comma separator after the element , To prevent confusion with parentheses in expressions . this Because Parentheses can represent tuples , It can also represent the priority operator in the expression , This is easy to produce ambiguity .

2、 Tuple access
Tuples have the same access as lists , Sure Directly use index to access single data in tuple term , You can also use the intercept operator to access child tuples . Access operators include “[]” and “[:]” Operator , Used to access a single data item in a tuple , Or a child tuple .
3、 How to update tuples
Tuples are immutable types , While the program is running Cannot insert and delete elements of tuples operation . But tuples can replace old tuples by constructing a new tuple , To insert and delete elements .
4、 Combination of tuples
You can put Multiple elements are combined into a tuple , The combined tuple element order remains unchanged . The merged tuple is a new tuple , The original tuple remains unchanged .
5、 Traversal of tuples
Tuples are traversed in the same way as lists , All applications for A loop statement traverses the elements of a tuple .
6、 Common methods for manipulating tuples
Just like the list , The methods that apply to lists also apply to tuple operations . But because of the immutability of tuples , Used for sorting lists 、 Replace 、 Add and other methods , Cannot use... In tuples . The main methods that can be used are to calculate the number of tuples 、 Find the maximum value in tuple 、 Find the minimum value in tuple .
7、 The immutability of tuples
The immutability of tuples can make tuples very inflexible , Because tuples are container objects , Many times, you need to modify the elements of the container , This is not allowed in tuples . Tuples can be said to be a supplement to list data , The immutability of data is also very important in program design . for example , When you need to pass data as a parameter to API, But I don't want to API When modifying parameters , You can pass a tuple type ; To define a group Key when , Tuple types can also be used . So we can say Tuples and lists are complementary data types .
Course summary
Tuples are the same as lists , Is also a container object , You can store different types of content . Tuples differ from lists in two ways . The first difference is that the declaration of tuples uses parentheses , The list uses square brackets , When declaring a tuple with only one element , You need to add an English comma after this element ; The second tuple after the assignment is different from the second tuple , You can't add... Like a list 、 Delete and modify elements , In other words, tuples cannot be modified during program operation .
Used for sorting lists 、 Replace 、 Methods such as adding do not apply to tuples , The main operations applicable to tuples are the combination of tuples 、 Traverse 、 Find the maximum and minimum values of tuples and other operation methods .
边栏推荐
- 在64位机器使用CMake编译32位程序
- 4.8 inquirer-autocomplete-prompt
- How to write attractive titles for short videos? Learn these tips to make your title more convincing
- Difference between function declaration and function expression
- showCTF Web入门题系列
- Mono fourni avec l'unit é 5 peut également supporter C # 6
- 【MGT】代码解读之model-MGT
- Global and Chinese market of Toro from 2022 to 2028: Research Report on technology, participants, trends, market size and share
- 2022-2028 global postoperative pressure suit industry research and trend analysis report
- Unity写多线程注意事项
猜你喜欢

Redis master-slave vulnerability and remote connection vulnerability

Joking Domain Driven Design (VI) -- Boundary context -- Design
![[MySQL performance optimization] - optimize query](/img/24/0b2abeeafb2583574cdcde98cfc559.jpg)
[MySQL performance optimization] - optimize query

Markdown rule for writing articles

解密FTP

2022-2028 global boom cylinder industry research and trend analysis report
![[yuanuniverse 3D competition]](/img/5e/386d2299ba62a78bd73982997d4df9.png)
[yuanuniverse 3D competition]

2022-2028 global section valve industry research and trend analysis report

Decrypt FTP

Summary of problems and errors encountered in tidb4.0.0 (tiup deployment)
随机推荐
Generic functions in kotlin
antd table长表格如何出现滚动条
Unity . Net framework
Leedcode 1 - sum of two numbers
What should I do if a white page appears during MySQL installation
php exec、 system 、shell_ Exec cannot be executed. There is no result. The result is nulld. Solution
[DB written interview 367] in the three-level schema structure of the database, the description of all data logical structures and features in the database is ()
FD:文件描述符
Global and Chinese market of carton folding and bonding machines 2022-2028: Research Report on technology, participants, trends, market size and share
Unity的网络请求_短连接
在64位机器使用CMake编译32位程序
leetcode:19. 删除链表的倒数第 N 个结点
面试经验---字节
Post-Process初级使用笔记(重要的几项)
Mono fourni avec l'unit é 5 peut également supporter C # 6
Global and Chinese market of electrical connectors 2022-2028: Research Report on technology, participants, trends, market size and share
函数声明和函数表达式的区别
SQL to check the disk usage of the database / table, kill the process and terminate the connection in tidb
2022-2028 global internal gear motor industry research and trend analysis report
CTF show WEB10