We've talked about Python data types before, but not in depth, and we've learned more about the use of Python data types in this section.
There are many ways to list data types, and we're here to introduce you
1.append
The Append method is used to append an object to the end of the list.
For example, the following code
In [1]: My_list = [1, 2, 3, 4]in [2]: My_list.append (5) in [3]: my_listout[3]: [1, 2, 3, 4, 5]
Note Append, like other methods, modifies only the list and does not return a value
2.clear
Method clear clears the contents of the list in place
For example, the following code
In [4]: My_list = [1, 2, 3, 4]in [5]: My_list.clear () in [6]: My_listout[6]: []
This function can also be achieved by slicing
In [7]: My_list = [1, 2, 3, 4]in [8]: my_list[:] = []in [9]: my_listout[9]: []
3.copy
Copy is used to replicate the list, but be aware that he simply associates a variable to this list
The following is simply an assignment for a simple copy
In [ten]: My_list = [1, 2, 3, 4]in [all]: Other_list = my_listin []: other_listout[12]: [1, 2, 3, 4]
For My_list and Other_list to point to a different list, you must use copy
For example, the following code
in [[]: My_list = [1, 2, 3, 4]in [+]: Other_list = my_list.copy () in []: other_listout[17]: [1, 2, 3, 4]in []: Other_ List[0] = ' one ' in [[]: other_listout[19]: [' One ', 2, 3, 4]in []: my_listout[20]: [1, 2, 3, 4]
You can see that the two list is pointing to a memory address that is not the same
4.count
Calculates how many times the specified element appears in the list
in [+]: word_list = [' to ', ' is ', ' or ', ' not ', ' to ', ' being ']in [$]: Word_list.count (' to ') out[22]: 2
Note that for multidimensional lists, things can be a little different
In [all]: Multi_list = [1, [1, 2], 1, [1, 2, 3]]in []: Multi_list.count (1) out[24]: 2In []: Multi_list.count ([up]) out[2 5]: 1
5.extend
You can attach multiple values to the end of the list, which means you can connect two lists, or you can use a list to extend another list
in [+]: a_list = [1, 2]in [+]: b_list = [3, 4]in []: A_list.extend (B_list) in []: a_listout[29]: [1, 2, 3, 4]
This looks a bit like the following lines of code, connecting two of lists
in [+]: a_list = [1, 2]in []: B_list = [3, 4]in [+]: A_list + b_listout[32]: [1, 2, 3, 4]
But it's important to note that the result is not directly assigned to the value
In addition to using the Extend method above, we can also use the slice operation
In [all]: A_list = [1, 2]in []: B_list = [3, 4]in [in]: A_list[len (a_list):] = B_listin []: a_listout[42]: [1, 2, 3, 4]
But I have to say, the readability is poor.
6.index
Method index finds the first occurrence of the specified value in the list
For example, the following code
in [+]: my_list = [1, 2, ' susmote ', ' ayesup ', ' ayesup ', ' Susmote ', 2, 1]in []: My_list.index (' Susmote ') out[44]: 2
To emphasize here, index just looks for the first occurrence of the index
7.insert
The Insert method inserts an object into the list
For example, the following code
In [the]: My_list = [1, 2, 3, 4, 6]in []: My_list.insert (4, 5) in []: my_listout[48]: [1, 2, 3, 4, 5, 6]
Note that the first parameter is the index, the second is the inserted value, and the value inserted in front of the index
You can also use slices to complete the above operations
In [Wuyi]: My_list = [1, 2, 3, 4, 6]in []: my_list[4:4] = [5]in []: my_listout[53]: [1, 2, 3, 4, 5, 6]
8.pop
Deletes an element, defaults to the last element, and finally returns the element
For example, the following code
In [si]: my_list = [1, 2, 3, 4, 5]in []: My_list.pop () out[55]: 5In []: my_listout[56]: [1, 2, 3, 4]in []: My_list.po P (0) out[57]: 1In [+]: my_listout[58]: [2, 3, 4]
Pop is the only list method that modifies the list and returns a value other than none.
Using pop to achieve a common data structure-stack (stack), this in the later article I will talk about, here do not elaborate
9.remove
Deletes the first element that is a specified value
For example, the following code
in [+]: word_list = [' to ', ' is ', ' or ', ' not ', ' to ', ' is ']in [max]: Word_list.remove (' to ') in [Max]: word_listout[61]: [' Be ', ' or ', ' not ', ' to ', ' being ']
10.reverse
Method reverse the elements in the list in reverse order
in [+]: my_list = [1, 2, 3, 4, 5]in []: My_list.reverse () in []: my_listout[64]: [5, 4, 3, 2, 1]
If you want to iterate the sequence in reverse order, you can use the function reversed. Instead of returning a list, this function returns an iterator (the iterator is described in detail in chapter 9th). You can use list to convert the returned object to a list.
For example, the following code
in [+]: my_list = [1, 2, 3, 4, 5]in []: List (Reversed (my_list)) out[66]: [5, 4, 3, 2, 1]
11.sort
Method sort is used to sort the list in place, but does not return a value
For example
In [the]: My_list = [7, 4, 3, 5, 8, 9, 1]in []: My_list.sort () in []: my_listout[70]: [1, 3, 4, 5, 7, 8, 9]
Because there is no return value, you can use the function sorted if you need to assign the sorted value to a variable conveniently.
in [+]: my_list = [7, 4, 3, 5, 8, 9, 1]in []: Other_list = sorted (my_list) in []: my_listout[73]: [7, 4, 3, 5, 8, 9, 1]in [All]: other_listout[74]: [1, 3, 4, 5, 7, 8, 9]
Note that for sort he has two optional parameters: Key and reverse
Key means the keywords you sort by
Reverse indicates whether to sort the list in reverse order, True or False
For example, the following two paragraphs of code
Key = Len
In [the]: word_list = [' Aardvark ', ' abalone ', ' Acme ', ' Add ', ' aerate ']in []: Word_list.sort (Key=len) in []: Word_listout [+]: [' Add ', ' Acme ', ' aerate ', ' abalone ', ' aardvark ']
Reverse = True
In [the]: My_list = [7, 4, 3, 5, 8, 9, 1]in []: My_list.sort (Reverse=true) in []: my_listout[80]: [9, 8, 7, 5, 4, 3, 1]
Welcome to visit my Blog Web www.susmote.com
13_python data type string strengthening _python programming path