In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you what the basic list of Python is. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Without saying much, let's take a look at it.
I. brief introduction
Each element in the list is assigned a number-its location, or index, with the first index 0, the second index 1, and so on.
To create a list, simply enclose the different data items separated by commas in square brackets. As follows:
List1 = ['Google',' Runoob', 1997, 2000]; list2 = [1,2,3,4,5]; list3 = ["a", "b", "c", "d"]; II. Access to the list
Use the subscript index to access the values in the list, and you can also intercept characters in square brackets, as shown below:
List1 = ['Google',' Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7]; print ("list1 [0]:", list1 [0]) print ("list2 [1:5]:", list2 [1:5]) output result: list1 [0]: Googlelist2 [1:5]: [2,3,4,5] III, update list
You can modify or update the data items in the list, or you can use the append () method to add list items, as shown below:
List = ['Google',' Runoob', 1997, 2000] print ("the third element is:", list [2]) list [2] = 2001print ("the third element after the update is:", list [2]) output the result: the third element is: 1997 the third element after the update is: 2001 IV, delete the list element
You can use the del statement to delete elements of a list, as shown in the following example:
List = ['Google',' Runoob', 1997, 2000] print (list) del list [2] print ("Delete third element:", list) output result: delete the third element: ['Google',' Runoob', 2000] V. Python list script operator Python expression result describes len ([1,2,3]) 3 length [1,2,3] + [4,5,6] [1,2,3,4,5] 6] combination ['hippie'] * 4 ['hippie', 'hippie'] Repeat 3 in [1, 2, 3] whether the True element exists in the list for x in [1, 2, 3]: print (x, end= ") 1 2 3 iteration 6, Python list interception and splicing
L = ['Google',' Runoob', 'Taobao']
The result of the Python expression describes that L [2] 'Taobao' reads the third element L [- 2]' Runoob' reads the penultimate element from the right: count from the rightL [1:] ['Runoob',' Taobao'] outputs all elements starting from the second element
The list also supports stitching operations:
Squares = [1, 4, 9, 16, 25] squares + [36, 49, 64, 81] [1, 4, 9, 16, 25, 36, 49, 64, 81 100] VII. Python list function & method 7.1function description len (list) list element number max (list) return list element maximum value min (list) return list element minimum value list (seq) convert tuple to list 7.2.Method description list.append (obj) adds a new object list.count (obj) to count the number of times an element appears in the list list.extend (seq) in the column Append multiple values from another sequence at once at the end of the table (extend the original list with a new list) list.index (obj) find the index position of the first match of a value from the list list.insert (index) Obj) inserts objects into the list list.pop (obj=list [- 1]) removes an element from the list (default last element) And return the value of the element list.remove (obj) remove the first match of a value in the list list.sort ([func]) sort the original list list.clear () empty the list list.copy () copy the list above is what the basic list of Python is, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.