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 is the definition and usage of Python list

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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

1. Definition and usage list

In Python, a list is a data structure made up of a series of elements in a specific order, that is, variables of the list type can store multiple data and can be repeated.

1.1 definition list

Use [] literal syntax to define variables, and multiple elements in the list are divided by commas

The sample code is as follows:

List1 = ["Hello", "Bowl week", "Hello"] list2 = [1,2,3,4 list1 5] print (list1) # ['Hello',' Bowl week', 'Hello'] print (list2) # [1,2,3,4 record5]

Use Python's built-in list to program other sequences. The sample code is as follows:

List1 = list (range (10)) list2 = list ("hello") print (list1) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print (list2) # ['hype,' eBay, 'lump,' o']

A list is a variable data type, that is, the elements of the list can be modified, which is significantly different from a string. After modifying the string type, a new string will be returned.

2. The values in the access list

If you access a value in the list, use the subscript index to access the value in the list and intercept characters in square brackets like a string. The example code is as follows:

List1 = ["Hello", "Bowl week", "Hello"] # Index of the list print (list1 [1]) # Bowl week # sliced print (list1 [1:3]) # ['Bowl week', 'Hello'] 2.1 operators of the list

Tables, like string types, also support concatenation, repetition, member operations, and so on.

The sample code is as follows:

List1 = ["Hello"] list2 = ["World"] list3 = [1,2,3,4,5] list4 = list (range (1,6)) list5 = list1 + list2 # ['Hello',' World'] print (list5) list6 = list1 * 3 # ['Hello',' Hello', 'Hello'] list7 = list3 * 2 # [1,2,3,4,5, 1,2,3,4 5] print (list6) print (list7) print ("W" in list1) # Falseprint ("W" in list2) # False# list comparison operation # two lists compare equality ratio is whether the elements at the corresponding index position are equal print (list3 = = list4) # Truelist8 = list (range (1,7) print (list3 = = list8) # False2.2 list element traversal

Traversing a list is the same as traversing a string. The sample code is as follows:

List1 = ["H", "e", "l", "l", "o"] # method 1for index in range (len (list1)): print (List1) # method 2for ch in list1: print (ch) 3, list method 3.1 add and remove elements

Go directly to the code

List1 = ["cute", "beautiful", "one bowl week"] # append () add elements list1.append ("lovely") print (list1) # ['cute',' beautiful', 'bowl week', 'lovely'] # insert () insert element list1.insert (2, "prefect") print (list1) # [' cute', 'beautiful',' prefect', 'bowl week at the specified index position in the list 'lovely'] # remove () delete specified element list1.remove ("lovely") print (list1) # [' cute', 'beautiful',' prefect', 'bowl week'] # pop () delete element list1.pop (2) print (list1) # ['cute',' beautiful', 'bowl week'] # clear () clear element list1.clear () print (list1) # []

List elements can also be deleted in Python using the del keyword, similar to pop, sample code ↓

List1 = ["cute", "beautiful", "Sweets"] del list1 [1] print (list1) # ['cute',' Sweets'] # delete the entire list del list1print (list1) # NameError: name 'list1' is not defined3.2 element location and number

Use index () to find the location of the element, and count () to count the number of occurrences of the element

List1 = ["beautiful", "cute", "beautiful", 'prefect', "beautiful", "one bowl week",' lovely'] # find the first occurrence of "beautiful" print (list1.index ("beautiful")) # find the last occurrence of "beautiful" after the fourth element print (list1.index ("beautiful") 3)) # count the number of "beautiful" occurrences print (list1.count ("beautiful")) # 33.3 sorting and reversing elements

Use the sort () method to sort the list elements, and the reverse () method to reverse the elements. Sample code ↓

List1 = ["cute", "beautiful", "one bowl week"] list2 = list (range (10)) # sort list1.sort () print (list1) # ['beautiful',' cute', 'bowl week'] # reverse list1.reverse () print (list1) # ['one bowl week', 'cute',' beautiful'] list2.reverse () print (list2) # [9, 8, 7, 6, 5, 4, 3, 2, 1 0] # modify the original list of the previous operation If the original data is not destroyed, you can use copy () to back up a copy of list3 = list2.copy () list3.sort () print (list2) # [9,8,7,6,5,4,3,2,1,0] print (list3) # [0,1,2,3,4,5,6,7,8,9] 3.4 Summary

4. the generation of the list.

Requirement: create a list of Cartesian products for the string 123 and the string ABC. The sample code is as follows:

Original method:

A = "123" b =" ABC "list1 = [] for x in a: for y in b: list1.append (x + y) print (list1) # ['1A','1B','1C','2A','2B','3A','3B','3C']

Generate column method:

A = "123" b =" ABC "list1 = [x + y for x in a for y in b] print (list1) # ['1A','1B','1C','2A','2C','2C','3A','3B','3C']

This method not only has less code, but also has better performance than ordinary for loop and append append method.

5. Nested list

Because the variables in the list can store multiple data types, when there is a list in the list, it is called the nesting of the list. The sample code is as follows:

List1 = ["cute", "beautiful", "one bowl week"], "cute", "beautiful", "one bowl week"] print (list1 [0]) # ['cute',' beautiful', 'bowl week'] print (list1 [1]) # cute# if you want to view the nested cute, you need to use multiple index values print (list1 [0] [0]) # cute

No matter how much is nested, it is the same.

At this point, the study of "what is the definition and use of the Python list" is over. 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