What are the three types of python sequence types?

angryTom
Release: 2020-02-25 16:45:28
Original
24667 people have browsed it

What are the three types of python sequence types?

What three types of python sequence types include

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: move Except one element, defaults to the last one.

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

Insertion: userlist.insert(3,555)

Sort: userlist.sort() The default is 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

Get Element: userlist[2]

Get coordinates: userlist.index(999)

Connection of list: extend() method. Or use to concatenate two lists. The two are different

Tuple: ordered immutable sequence

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

Modification: The value cannot be modified

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

Delete: (None for immutable sequence) This attribute)

Insert: (Immutable sequence has no such attribute)

Sort: Only sorted(userlist) can be used for sorting

Reverse: (Immutable sequence has no such attribute) This attribute)

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

Get the element: tuple1[4]

Get 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 Create a dictionary using a function: dict1 = dict([('a','001'),('b','002'),('c','003'),('d','004')] )

Modification: 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 k-v unchanged, and when the key does not exist, add the k-v.

Delete: There is no remove() function for the dictionary, 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

for insertion: the dictionary has no index coordinates. Only addition, no insertion

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

Reverse: (Unordered and cannot be reversed)

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

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

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

Deduplication: key is unique, no need to deduplicate

String – tuple –List-Dictionary type conversion

1. Convert tuple to list: list()

2. Convert list to tuple: tuple()

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

4. Convert list tuples to dictionaries: dict()

numerouspython training videos, all on the Python Learning Network, welcome to online learning!

The above is the detailed content of What are the three types of python sequence types?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!