当前位置:网站首页>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 .

原网站

版权声明
本文为[Luckly Sister]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221453435824.html