Detailed explanation of python sequence types

What are the three types of python sequence

Python sequence types include: list, tuple, dictionary

List: ordered variable sequence

Create: userlist = [1,2,3,4,5,6]

Modify: userlist[5] = 999

Add: userlist.append(777)

Delete: userlist.remove(4) or del(userlist[3])

Pop method: remove an element, the default is the last one.

userlist.pop(3) removes the third element and returns the value.

Insert: userlist.insert(3,555)

Sort: userlist.sort() defaults to ascending order. userlist.sort(reverse=True) uses descending order. Or use sorted(userlist) to sort

Reverse: userlist.reverse()

Search: userlist.index(3) or use in reserved words to search

Take element: userlist[2]

Take the coordinates: userlist.index(999)

The connection of the list: extend() method. Or use + to connect two lists. The two are different

Tuple: ordered immutable sequence

Create: tuple1=(1,2,3,4,5,6)

Modify: the value cannot be modified

Add: There is no append function, it can only be added by assignment: tuple2=(tuple1,7,8,9)

Delete: (the immutable sequence does not have this attribute)

Insertion: (the immutable sequence does not have this attribute)

Sorting: can only be sorted using sorted (userlist)

Reverse: (Immutable sequence does not have this attribute)

Search: userlist.index(3) or use in reserved words to search

Take element: tuple1[4]

Take the coordinates: tuple1.index(3)

Deduplication: set(tuple1)

Unpacking: a,b,c,d,e,f = tuple1

Dictionary: Unordered variable sequence

Create: dict1={'a':'001','b':'002','c':'003','d':'004'} Or use a function to create a dictionary: dict1 = dict([(' a','001'),('b','002'),('c','003'),('d','004')))

Modify: the value cannot be modified

Add: direct assignment: dict1['f'] = '006'; or use the setdefault() function to add dictionary elements: dict1.setdefault('e','005'), when the key already exists, keep the original The kv remains unchanged. When the key does not exist, the kv is added.

Delete: The dictionary does not have the remove() function, but the kv of the dictionary can be deleted with the del() function: del(dict1['e']). You can also use the pop() method to delete the specified element. Since the dictionary is unordered, pop() will not delete the last element by default. You must specify the key

Insertion: The dictionary has no index coordinates, only adding, not inserting

Sorting: The dictionary has no index coordinates, so it is also unordered, and the value can only be found by key. But it can be sorted by other methods: for k in sorted(dict1): print(k,dict1[k])

Reversal: (disordered cannot be reversed)

Search: dict1['c'] or use in reserved words to search. Or use the items() method to convert each pair of kv in the dictionary into a tuple for convenient search

Get element: dict1['c'] or use dict1.get('c')

Take the coordinates: the key is unique, the value is not unique, it can only be found by looping the convenience dictionary

De-duplication: unique key, no need to de-duplicate

String-tuple-list-dictionary type conversion

1、 Convert tuples to lists: list()

2、 The list is turned into a tuple: tuple()

3、 Convert dictionaries to lists and tuples: dict1.items()

4、 The list ancestor is converted to a dictionary: dict()

Knowledge point expansion:

Sequence types in Python include:

Container sequence

Container sequence

Flat sequence

Variable sequence

Immutable sequence

Once created, it cannot be modified.

The above is the detailed content of the detailed explanation of python sequence types. For more information about the three types of python sequence types, please pay attention to other related articles on ZaLou.Cn!

Recommended Posts

Detailed explanation of python sequence types
Detailed explanation of data types based on Python
Detailed explanation of python backtracking template
Detailed explanation of Python IO port multiplexing
Detailed explanation of -u parameter of python command
Detailed explanation of Python guessing algorithm problems
Detailed explanation of the principle of Python super() method
Detailed explanation of python standard library OS module
Detailed explanation of the usage of Python decimal module
Detailed explanation of how python supports concurrent methods
Detailed explanation of the principle of Python function parameter classification
Detailed explanation of the principle of Python timer thread pool
Detailed explanation of the implementation steps of Python interface development
Detailed explanation of common tools for Python process control
Detailed explanation of Python web page parser usage examples
Detailed explanation of the attribute access process of Python objects
Detailed explanation of the remaining problem based on python (%)
Detailed implementation of Python plug-in mechanism
Detailed explanation of ubuntu using gpg2
Python error handling assert detailed explanation
Detailed usage of dictionary in Python
Detailed analysis of Python garbage collection mechanism
Comprehensive summary of Python built-in exception types
Python from attribute to property detailed explanation
Detailed usage of Python virtual environment venv
Python learning-variable types
7 features of Python3.9
Detailed explanation of the use of pip in Python | summary of third-party library installation
02. Python data types
Ubuntu20.04 install Python3 virtual environment tutorial detailed explanation
Detailed explanation of building Hadoop environment on CentOS 6.5
Detailed examples of using Python to calculate KS
Detailed explanation of static DNS configuration method in Ubuntu
Detailed explanation of Centos 7 system virtual machine bridging mode
Detailed explanation of static DNS configuration under Ubuntu system
Detailed Python IO programming
Detailed Python loop nesting
Basics of Python syntax
Python basic data types
Basic syntax of Python
Basic knowledge of Python (1)
Python—requests module detailed explanation
Prettytable module of python
Python basic data types
09. Common modules of Python3
Equal Insurance Evaluation: Detailed Explanation of Centos Timeout Exit
Detailed explanation of CentOS7 network setting tutorial in vmware
Detailed explanation of Spark installation and configuration tutorial under centOS7
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Python analysis of wav files
Consolidate the foundation of Python(6)
Analysis of JS of Python crawler
python king of glory wallpaper
Consolidate the foundation of Python(5)
Python implementation of gomoku program
Analysis of Python Sandbox Escape
Some new features of Python 3.10
Deep understanding of Python multithreading