Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the python sequence types?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of python sequence types, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this python sequence type article. Let's take a look at it.

What are the three types of python sequences

Python sequence types include: list, tuple, dictionary

List: ordered variable sequence

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

Modification: userlist [5] = 999

Add: userlist.append

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

Pop method: removes one element and defaults to the last one.

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

Insert: userlist.insert (3555)

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

Reverse: userlist.reverse ()

Find: userlist.index (3) or use in reserved words to find

Take element: userlist [2]

Take coordinates: userlist.index (999)

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

Tuples: ordered immutable sequences

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

Modifying: values cannot be modified

Add: there is no append function, you can only add it by assignment: tuple2= (tuple1,7,8,9)

Delete: (immutable sequence does not have this attribute)

Insert: (immutable sequence does not have this attribute)

Sorting: sorting can only be done using sorted (userlist)

Reverse: (immutable sequence does not have this attribute)

Find: userlist.index (3) or use in reserved words to find

Take element: tuple1 [4]

Take coordinates: tuple1.index (3)

Weight removal: set (tuple1)

Unpack: a _

Dictionary: disordered variable sequence

Create: dict1= {'astatine / 001') / or use the function to create a dictionary: dict1=dict ([(' astatine] /'), (/ /) / (/ /) / /)

Modifying: values cannot be modified

Add: directly assign: dict1 ['f'] = '006], or use the setdefault () function to add a dictionary element: dict1.setdefault (' estrangement journal 005'). If key already exists, the original KMuv will remain unchanged, and when key does not exist, the KMuv will be added.

Delete: the dictionary does not have a remove () function, but you can delete the dictionary's kv:del (dict1 ['e']) by the del () function. You can also delete the specified element using the pop () method. Because the dictionary is unordered, pop () does not delete the last element by default. You must specify key.

Insert: dictionary has no index coordinates, only add, no insert

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

Reverse: (disorder cannot be reversed)

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

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

Take coordinates: key is unique, value is not unique, and can only be found through a circular convenience dictionary.

Weight removal: key is unique and does not need to be weighed.

Types of string-tuple-list-dictionary convert each other

1. Convert tuples to lists: list ()

2. The list is changed to tuple: tuple ()

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

4. The list meta-ancestor is changed to a dictionary: dict ()

This is the end of the article on "what are the types of python sequences?" Thank you for reading! I believe you all have a certain understanding of "what are the python sequence types". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report