In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is the use of the Python list?" in the operation of actual cases, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Introduction to the list (1) what is a list
The list consists of a series of elements in a particular order. These elements can be: numbers, letters, strings, and other types of data. In Python, the list is represented by square brackets [] and the elements are separated by commas. Here is a simple example of a column:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] print (week_list)
If you use the print () function, Python will print the internal representation of the list, including square brackets:
['Monday',' Tuseday', 'Wednseday',' Thursday', 'Friday',' Saturday', 'Sunday']
If you want to output each element of the list directly, you have to learn how to access the elements of the list.
(2) access list elements
A list is an ordered collection, so to access any element of the list, you only need to know where that element is in the list. To access a list element, you can indicate the name of the list, indicate the index of the element, and place it in square brackets. For example, we want to get the Friday in the list above:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] print (Week_list [4])
The above demonstrates the syntax of an access list element. When you request to get a list element, Python returns only the value of that element, not square brackets and quotation marks:
Friday
Of course, if you want to output each element in the list in turn, you can do it through for:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] for day in Week_list: print (day)
Loop through the list through for, outputting each element in turn:
MondayTusedayWednsedayThursdayFridaySaturdaySunday (3) nested list
Nested lists create other lists in the list, such as:
List1 = [1,2,3] list2 = ["Cao Cao", "Sun Quan", "Liu Bei"] list = [list1, list2] print (list)
A nested list is equivalent to an element in a list is a list:
[[1, 2, 3], ['Cao Cao', 'Sun Quan', 'Liu Bei']
When you want to take an element in the list, such as "Cao Cao", you need to learn the use of the index.
Index, Segmentation and list replication (1) Index
The index value is related to the position of the element. In Python, the index of the first list element is 0, not 1. According to this simple counting method, to access any element of the list, you can subtract its position by 1 and use the result as an index. For example, to access the 10th list element, use index 9.
For a normal list, when we want to access its fifth element:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] print (Week_list [4])
Program input:
Friday
For the absconding list, when we want to access the elements, such as "Cao Cao" in the above list, we use the following ways:
List = [[1,2,3], ['Cao Cao', 'Sun Quan', 'Liu Bei']] print (list [1] [0])
Program input:
Cao Cao
Where the index value 1 in list [1] [0] indicates the second list, and the index value 0 indicates the first element in the second list.
(2) slice
Individual list elements can be accessed through the index, and all or part of the list elements can be processed by slicing. To create a slice, specify the index of the first and last element to use. The main note is that Python stops when it reaches the element in front of the second index you specify.
1. For example, to output the first three elements in the list, you need to specify index 0room3, which outputs elements of 0, 1, and 2, respectively.
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] print (Week_list [0:3])
Take the first three elements of the list by slicing:
['Monday',' Tuseday', 'Wednseday']
two。 You can also get any subset of the list. For example, if you want to extract the 2nd and 6th elements of the list, you can specify the starting index as 1 and the ending index as 6:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] print (Week_list [1:6])
Output Tuesday to Saturday:
['Tuseday',' Wednseday', 'Thursday',' Friday', 'Saturday']
3. If you do not specify the first index, Python automatically starts at the beginning of the list:
Week_list = [Monday "," Tuseday "," Wednseday "," Thursday "," Friday "," Saturday "," Sunday "] print (Week_list [: 6])
Output Monday to Saturday:
['Monday',' Tuseday', 'Wednseday',' Thursday', 'Friday',' Saturday']
4. If you want to take all the elements from the third element to the end of the list, you can also do the following:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] print (Week_ list [2:])
Return Wednesday to Sunday:
['Wednseday',' Thursday', 'Friday',' Saturday', 'Sunday']
5. If you want to traverse some elements of the list, you can use slices in the for loop.
Week_list = [Monday "," Tuseday "," Wednseday "," Thursday "," Friday "," Saturday "," Sunday "] for day in Week_list [: 3]: print (day)
Iterate through the list of 1 to 3 elements and output:
MondayTusedayWednseday (3) list replication
In the development process, we often need to create entirely new lists based on existing lists. Here's how replication lists work and a situation in which replication lists can be of great help.
To copy a list, create a slice that contains the entire list by omitting both the starting index and the terminating index [:]. This allows Python to create a slice that starts with the first element and ends with the last element, that is, copying the entire list.
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] Week_list_copy1 = Week_listWeek_list_copy2 = Week_list [:] print ("Week_list:", id (Week_list)) print ("Week_list_copy1:", id (Week_list_copy1)) print ("Week_list_copy2:", id (Week_list_copy2))
By copying with or without [:], the id of the list is as follows:
Week_list: 298963473096Week_list_copy1: 298963473096Week_list_copy2: 298963418440
As can be seen from the result: Week_list_copy1 is the same as the id of the original list Week_list, indicating that Week_list_copy1 and the original list point to the same list; Week_list_copy2 is different from the id of the original list Week_list, indicating that a new list is created by copying it in the way of [:].
Number of list elements, maximum value, minimum value, conversion
In Python, there are some built-in functions that make it easy to work with data of this type, such as lists.
(1) return the number of elements
For a normal list, returns the number of list elements:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] print (len (Week_list))
Program output:
seven
For nested lists, returns the number of lists in the list:
List = [[1,2,3], ['Cao Cao', 'Sun Quan', 'Liu Bei'] print (len (list))
Program output:
2 (2) returns the maximum value of the element
The max () method returns the maximum value in the list element:
List1, list2 = ['Google',' Baidu', 'Taobao'], [1,2,3] print ("list1 maximum element value:", max (list1)) print ("list2 maximum element value:", max (list2))
Output the maximum value of the element in the list:
List1 maximum element value: Taobaolist2 maximum element value: 3 (3) returns the minimum element value
The min () method returns the minimum value in the list element:
List1, list2 = ['Google',' Baidu', 'Taobao'], [1,2,3] print ("list1 minimum element value:", min (list1)) print ("list2 minimum element value:", min (list2))
Output the minimum value of the element in the list:
List1 minimum element value: Baidulist2 minimum element value: 1 (4) converts tuples to lists
The list () method is used to convert tuples or strings into lists:
Str = "Hello Python" list2 = list (str) print ("list elements:", list2) Tuple = (123,' Google', 'baidu',' Taobao') list1 = list (Tuple) print ("list elements:", list1)
Program result:
List element: ['Google',' baidu', 'Taobao']
Note: tuples are very similar to lists, except that the element values of tuples cannot be modified. Tuples are placed in parentheses and lists are placed in square brackets.
IV. Modify, add and delete operations (1) modify list elements
The syntax for modifying list elements is similar to that for access 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 for that element. For example, let's take the first value in the list:
Week_list = [Monday "," Tuseday "," Wednseday "," Thursday "," Friday "," Saturday "," Sunday "] Week_list [0] =" Monday "print (Week_list)
Change the Monday in the list to Monday:
['Tuseday',' Wednseday', 'Thursday',' Friday', 'Saturday',' Sunday']
Of course, you can modify the value of any list element, not just the first element.
(2) add elements at the end of the list
When you add a new element to a list, the easiest way is to use append () to add the element to the end of the list:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] Week_list.append (100) print (Week_list)
Add 100 to the end of the list:
['Monday',' Tuseday', 'Wednseday',' Thursday', 'Friday',' Saturday', 'Sunday', 100]
You can easily create a list with append (). For example, you can create an empty list and then add elements using a series of append () statements. Let's create an empty list and add elements to it:
Week_list = [] Week_list.append (1) Week_list.append (2) Week_list.append (3) print (Week_list)
Program output:
[1, 2, 3] (3) insert elements in the list
Use the method insert () to add a new element anywhere in the list. To do this, you need to specify the index and value of the new element:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] Week_list.insert (0100) print (Week_list)
Add 100 at the beginning of the list:
[100,100, 'Monday',' Tuseday', 'Wednseday',' Thursday', 'Friday',' Saturday', 'Sunday'] (4) Delete list elements
During data processing, we often need to delete one or more elements from the list. Python provides a way to delete elements from the list in 4:
1. Use the del statement to delete the element. If you know where the element you want to delete is in the list, you can use the del statement.
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] del Week_list [0] print (Week_list)
Delete the first element in the list:
['Tuseday',' Wednseday', 'Thursday',' Friday', 'Saturday',' Sunday']
two。 Delete the element using the method pop (). The method pop () removes the element at the end of the list and allows you to continue using it.
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] Week_list.pop () print (Week_list)
Delete the last element in the list:
['Monday',' Tuseday', 'Wednseday',' Thursday', 'Friday',' Saturday']
3. Delete the element anywhere in the list. In fact, using pop (), you can also delete an element anywhere in the list by specifying the index of the element to be deleted in parentheses.
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] Week_list.pop (1) print (Week_list)
Delete the second element in the list:
['Monday',' Wednseday', 'Thursday',' Friday', 'Saturday',' Sunday']
Note: if you are not sure whether to use the del statement or the pop () method, here is a simple criterion: if you want to delete an element from the list and no longer use it in any way, use the del statement; if you want to delete the element and continue to use it, use the method pop ().
4. Deletes the element based on the value. Sometimes you don't know the location of the value you want to remove from the list. If you only know the value of the element you want to delete, use the method remove ().
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] Week_list.remove ("Monday") print (Week_list)
Delete the element "Monday" from the list:
['Tuseday',' Wednseday', 'Thursday',' Friday', 'Saturday',' Sunday']
Note: remove () removes the first match of a value in the list
List operator (1) splicing operator
It can also be used as a stitching operator in Python,+ to concatenate different lists into one list:
List1 = [1,2,3] list2 = [4,5,6] print (list1 + list2)
After stitching together into a list:
[1, 2, 3, 4, 5, 6] (2) copy operator
Using the * operator, you can easily copy list elements:
List = [1,2,3] print (list * 2)
Copy all the elements in the list to 2x:
[1, 2, 3, 1, 2, 3]
By the same token, it can be replicated many times.
VI. List sorting
In the actual development process, sometimes you want to keep the original order of the list elements, and sometimes you need to adjust the order. Python provides many ways to organize lists, which can be selected on a case-by-case basis.
(1) permanent sorting sort ()
Sort () makes it easier for you to sort the list. Sort the elements in the list alphabetically below:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] Week_list.sort () print (Week_list)
Sorting through sort () permanently changes the order of the elements in the original list:
['Friday',' Monday', 'Saturday',' Sunday', 'Thursday',' Tuseday', 'Wednseday']
When you want to sort in descending order, simply pass the parameter reverse=True to the sort () method, as follows:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] Week_list.sort (reverse=True) print (Week_list)
The descending sort results are as follows:
['Wednseday',' Tuseday', 'Thursday',' Sunday', 'Saturday',' Monday', 'Friday'] (2) temporary sorting sorted ()
Sometimes if you want to sort the list and keep the order of the original list elements, you can use the sorted () function.
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] Week_list_sorted = sorted (Week_list) print (Week_list_sorted) print (Week_list)
Compare the input results after sorting:
['Friday',' Monday', 'Saturday',' Sunday', 'Thursday',' Tuseday', 'Wednseday'] [' Monday', 'Tuseday',' Wednseday', 'Thursday',' Friday', 'Saturday',' Sunday']
You can see that sorted () does not change the order of the elements in the original list.
7. Other member functions (1) count the number of occurrences of an element count ()
The count () method is used to count the number of times an element appears in the list. For example:
List = [1,2,2,2,3] print ("number of 2 in the list:", list.count (2))
Running result:
The number of 2 in the list: 3 (2) find the index position of the first match of a value from the list index ()
The index () function is used to find the index position of the first match of a value from the list. For example, find the index of 2 in the list:
List = [1, 2, 2, 2, 3] print ("Index of 2 appearing for the first time in the list:", list.index (2))
Running result:
Index of 2 in the list that appears for the first time: 1
Alternatively, you can specify that the search starts somewhere in the list:
List = [1, 2, 2, 2, 3, 2, 3] print ("starting with the fifth element, the index 2 in the list appears for the first time:", list.index (2,4))
Start the search with an element with an index value of 4, and the result is:
Starting with the fifth element, the index that 2 in the list appears for the first time: 5 (3) the element reverse () in the reverse list
The reverse () function is used to reverse the elements in the list. When we want to reverse output the elements in the list:
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] Week_list.reverse () print (Week_list)
After reversing the position of the elements in the list, the output is:
['Sunday',' Saturday', 'Friday',' Thursday', 'Wednseday',' Tuseday', 'Monday'] (4) clear the list clear ()
The clear () function is used to clear the list, similar to del a [:].
Week_list = ["Monday", "Tuseday", "Wednseday", "Thursday", "Friday", "Saturday", "Sunday"] Week_list.clear () print ("after the list is cleared:", Week_list)
After the list is cleared:
After the list is cleared: [] (5) copy the list copy ()
The copy () function is used to copy the list, similar to the way it is copied through the index [:] mentioned earlier.
Week_list = [Monday "," Tuseday "," Wednseday "," Thursday "," Friday "," Saturday "," Sunday "] Week_list_copy1 = Week_list.copy () print (" Week_list: ", id (Week_list)) print (" Week_list_copy (): ", id (Week_list_copy1))
Running result:
Week_list: 250643256008Week_list_copy (): 250643201352
You can see that copying using the copy () function is similar to copying through [:], creating a new list.
This is the end of the content of "what is the use of the Python list?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 226
*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.