In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article, "what are the methods of operation of the python list?", so the editor summarizes the following, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what are the methods of operation of the python list?"
A list is made up of a series of elements in a particular order! The list is the most commonly used Python data type and can appear as a comma-separated value in square brackets. The data items of the list do not need to have the same type!
1. The list of access list elements is an ordered collection, so to access any element of the list, simply tell python the location (index) of that element. List = ['su liang','hacker','ice'] print (list [0] .title ()) # result: Su Liangprint (list [1] .upper ()) # result: HACKERprint (list [2] .lower ()) # result: ice
Here the element returned by python does not contain square brackets, and the title method allows the first letter to be capitalized. The upper method makes all uppercase and the lower method makes all lowercase! These methods can make the elements we access more concise!
1.1 the index starts at 0 instead of 1.
In python, the element of the first list has an index of 0 instead of 1. 0. Most programming languages are defined in the same way. It has been demonstrated for you in the above example. Python provides special syntax for accessing the last element, which allows python to access the last element by specifying the index as-1.
List = ['su liang','hacker','ice'] print (list [- 1]) # result: iceprint (list [- 2]) # result: hacker2. Most of the lists created by modifying, adding, and deleting elements are dynamic, which means that you can add, delete, modify, query, and so on. 2.1 modify list elements
To modify a list element, specify the name of the list and the index of the element you want to modify, and then specify the new value of the element.
List = ['su liang','hacker','ice'] list [1] = 'hacker707'print (list) # result: [' su liang',' hacker707', 'ice'] 2.2 add elements to the list
In many cases, we need to constantly add new elements to the list. There are mainly the following methods.
2.2.1 add (append) at the end
The easiest way to add an element to a list is to attach the append () method, which allows you to add an element to the end of the list.
X = [] def list (name): global x x.append (name) print (x) while True: name = input ('enter name:') list (name)
Results:
2.2.2 add (insert) anywhere
Use the insert () method to add indexes and values to add elements anywhere in the list.
List = ['su liang','hacker','ice'] list.insert (1Magneto Kiko') print (list) # result: ['su liang',' kiko', 'hacker','ice'] 2.3 remove elements from the list
In many cases, we need to constantly delete some elements from the list. There are mainly the following methods.
2.3.1 Delete with del statement
If you know where the element you want to delete is in the list, you can use the de statement.
List = ['su liang', 'none',' kiko', 'hacker',' ice'] print (list.pop ()) # result: iceprint (list) # result: ['su liang', 'none',' kiko', 'hacker'] 2.3.2 use pop () method to delete
The method pop () deletes the element at the end of the list and allows you to continue using it.
List = ['su liang', 'none',' kiko', 'hacker',' ice'] print (list.pop ()) # result: iceprint (list) # result: ['su liang', 'none',' kiko', 'hacker'] 2.3.3 elements anywhere in the pop-up list
In fact, you can use pop to delete anywhere in the list, and the value needs to be indexed in parentheses.
List = ['su liang', 'none',' kiko', 'hacker',' ice'] x = list.pop (3) print (x) # result: hacker2.3.4 deletes the element based on the value (remove)
Sometimes when we don't know where the element is in the list, but only know the value of the element, we can delete it using the remove () method.
List = ['su liang', 'none',' kiko', 'hacker',' ice'] list.remove ('none') print (list) # result: [' su liang', 'kiko',' hacker', 'ice'] 3. Organize the list in the list you create, the order of the elements in which you can't predict, sometimes you need to keep the original sort of list elements, and sometimes you need to adjust the order. Python provides many ways to organize lists, which can be used on a case-by-case basis. 3.1 use the sort () method to permanently sort the list
When using the sort method, the default is from small to large, sorting from a to z, and you can still reverse the order by adding reverse=True in parentheses.
The sort at this time is to sort the list permanently, that is, it does not retain the original order of the list!
List = ['su liang', 'none',' kiko', 'hacker',' ice'] list.sort () print (list) # result: ['hacker',' ice', 'kiko',' none','su liang'] list.sort (reverse=True) print (list) # result: ['su liang', 'none',' kiko', 'ice',' hacker'] 3.2 use function sorted () to temporarily sort the list
Sorted retains the original list sequence compared to sort. If you want to reverse the order, just add the reverse parameter.
List = ['su liang', 'none',' kiko', 'hacker',' ice'] list2 = sorted (list) print (list2) # result: ['hacker',' ice', 'kiko',' none','su liang'] print (list) # result: ['su liang', 'none',' kiko', 'hacker',' ice'] 3.3 print list backwards (reverse)
To reverse the order of list elements, use the method reverse (). Note: this does not print the list elements in order, but reverses the original list elements. The reverse method also permanently changes the order of the list, and if you want to restore it, call the method again on the list.
List = [2, 5, 6, 4, 4] list.reverse () print (list) # result: [7, 8, 4, 6, 5, 2] 3.4 determine the list length (len)
Use the len function to quickly get the length of the list.
List = ['su liang', 'none',' kiko', 'hacker',' ice'] n = len (list) print (n) # result: 5 above is about the content of this article on how to operate the python list. I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, 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.