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

How to use the common functions of Python list

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

Share

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

This article mainly introduces "how to use the common functions of the Python list". In the daily operation, I believe many people have doubts about how to use the common functions of the Python list. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to use the common functions of the Python list." Next, please follow the editor to study!

Introduction

Append ()

Grammar

List.append (element)

Parameters.

Element: any type of element

Add an element at the end of the list

Name_list = ['zhangsan',' lisi', 'wangwu'] name_list.append (' zhaoliu') print (name_list)

Output:

['zhangsan',' lisi', 'wangwu',' zhaoliu']

You can add "any type" data at the end of the list, such as adding a list to the list

Name_list = ['zhangsan',' lisi', 'wangwu'] name_list.append ([1m 2m 3]) print (name_list)

Output:

['zhangsan',' lisi', 'wangwu', [1,2,3]]

The append () function adds "address". When the added list changes, the added list will also "change synchronously".

List1 = ['zhangsan'] a = [1Magne 2jue 3] list1.append (a) # list list1 add list aprint (list1) a.append (4) # list a changes list1.append (a) print (list1) # list list1 also changes synchronously

Output:

['zhangsan', [1,2,3]]

['zhangsan', [1,2,3,4], [1,2,3,4]]

Add two concepts:

Shallow copy: copy a reference to this object

Deep copy: copying the contents of an object

"Deep copy" can solve the synchronization problem of adding list in append () function.

List1 = ['zhangsan'] a = [1Magne 2je 3] list1.append (copy.deepcopy (a)) # Deep copy list aprint (list1) a.append (4) # Table a has changed list1.append (copy.deepcopy (a)) # Deep copy list aprint (list1) # list list1 is no longer synchronized

Output:

['zhangsan', [1,2,3]]

['zhangsan', [1,2,3], [1,2,3,4]]

The difference between append () function and extend () function

1.append () adds the element "reference", while extend () adds the element's "value"

2.append () can add "any type" element, while extend () can only add "sequence"

For example, when you add a list, append () adds the entire list, while extend () adds only the values in the list

List1 = ['zhangsan',' lisi', 'wangwu'] list2 = [' zhangsan', 'lisi',' wangwu'] list1.append ([1Magne 2 list1 3]) print ('append adds the whole list:', list1) list2.extend ([1 Magi 2 list1 3]) print ('extend add list value:', list2)

Output:

Append adds the entire list: ['zhangsan',' lisi', 'wangwu', [1,2,3]]

Value of extend add list: ['zhangsan',' lisi', 'wangwu', 1, 2, 3]

Extend ()

Grammar

List.extend (iterable)

Parameters.

Iterable: any iterable object (list, meta-ancestor, etc.)

Add list content to the "end" of the list

List1 = ['zhangsan',' lisi', 'wangwu'] list1.extend ([1Magazine 2Magazine 3]) # add list print (list1) list1.extend ((4meme 5Jing 6)) # add meta-ancestor print (list1) at the end of the list

Output:

['zhangsan',' lisi', 'wangwu', 1,2,3]

[zhangsan', 'lisi',' wangwu', 1, 2, 3, 4, 5, 6]

The "string" in Python is essentially an array, which is an iterable object and can be added using extend ().

List1 = ['zhangsan',' lisi', 'wangwu'] list1.extend (' abc') # add the string print (list1) at the end of the list

Output:

['zhangsan',' lisi', 'wangwu',' await, 'baked,' c']

Insert ()

Grammar

List.insert (index, element)

Parameters.

Index: numeric, specifying where to insert

Element: any type of element to be inserted

The insert () function can add "any type"

List1 = ['zhangsan',' lisi'] list1.insert (2, 2) # add plastic print (list1) list1.insert (3, 'str') # add string print (list1) list1.insert (4, [1 list1) list1.insert (5, (4) # add dictionary print (list1)

Output:

['zhangsan',' lisi', 2]

['zhangsan',' lisi', 2, 'str']

['zhangsan',' lisi', 2, 'str', [1,2,3]]

['zhangsan',' lisi', 2, 'str', [1,2,3], (4,5,6)]

Insert also has the problem of "list synchronization". Just change it to "soft copy".

List1 = ['zhangsan',' lisi'] a = [1Jing 2 lisi' 3] list1.insert (2, a) # add list aprint (list1) a.append (4) # list a changes print (list1) # list changes synchronously

Output:

['zhangsan',' lisi', [1,2,3]]

['zhangsan',' lisi', [1,2,3,4]]

Pop ()

Grammar

List.pop (index)

Parameters.

Index: numeric, index location to be deleted. Default is-1.

Return value: deleted valu

When no parameter is passed, the "last" value is deleted by default

List1 = [1,2,3,4] list1.pop () print (list1)

Output:

[1, 2, 3]

Delete the element at the specified location

List1 = [1,2,3,4] list1.pop (1) # Delete the second value print (list1)

Output:

[1, 3, 4]

"return" deleted element

List1 = [1,2,3,4] a = list1.pop (1) # Delete the second value and return the deleted value print (a)

Output:

two

Remove ()

Grammar

List.remove (element)

Parameters.

Element: any data type

The remove () function deletes the "normal type" element

List1 = ['zhangsan',' lisi', 1,2] list1.remove (1) # Delete numeric print (list1) list1.remove ('zhangsan') # Delete string print (list1)

Output:

['zhangsan',' lisi', 2]

['lisi', 2]

The remove () function deletes the object Type element

List1 = [1,2, [3,4], (5,6)] a = [3,4] b = (5,6) list1.remove (a) # delete list type print (list1) list1.remove (b) # delete meta-ancestor type print (list1)

Output:

[1,2, (5,6)]

[1, 2]

The "premise" of the remove () function to delete an object type is that the element in the list is an object type, and an error will be reported in the following way

List1 = [1pr 2, 3, 4, 5] list1.remove ([1p2])

Output:

Traceback (most recent call last):

File "E:/data/PrCharm/test1/55.py", line 2, in

List1.remove ([1BI 2])

ValueError: list.remove (x): x not in list

# value error: the value to be deleted is not in the list

The remove () function "one at a time" can only delete the "one" element in an one-dimensional array, not even by itself.

List1 = [1,2,3,4,5] print ('am I in myself?' , list1 in list1) list1.remove (list1)

Output:

Traceback (most recent call last):

File "E:/data/PrCharm/test1/55.py", line 3, in

List1.remove (list1)

ValueError: list.remove (x): x not in list

Am I in myself? False

At this point, on the "Python list of common functions how to use" the end of the study, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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