Here is a list of commonly used methods and small examples:
1. Append
To add elements at the end of the list, add elements at the end of the list and note several points:
A. The parameters added in append are as a whole
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.append (List ("Tiger"))
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', [', ' t ', ' I ', ' g ', ' e ', ' R ']]
The resulting value is not: [' s ', ' C ', ' o ', ' t ', ' t ', ' ', ' t ', ' I ', ' g ', ' e ', ' R ']
If you want to append this method, you can try the Shard assignment (or the Extend method as mentioned below):
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name[len (name):] = List ("Tiger") #从末尾追加
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', ', ' t ', ' I ', ' g ', ' e ', ' R ']
B.append only one element can be added at once
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.append ("A", "B") #添加多个元素即将报错
Traceback (most recent):
File "", Line 1, in?
Typeerror:append () takes exactly one argument (2 given)
>>> name.append ("A")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', ' A ']
2. Count
Count the number of occurrences of an element in a list
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.count (' s ')
>>> name.count ("T")
>>> name.count ("A")
>>> name.append (List ("Python"))
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', [' P ', ' y ', ' t ', ' h ', ' o ', ' n ']
>>> name.count ([' P ', ' y ', ' t ', ' h ', ' o ', ' n '])
3. Extend
Append multiple values from another sequence in the original list
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.extend (List ("Tiger"))
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', ', ' t ', ' I ', ' g ', ' e ', ' R ']
Of course, we can use Shard assignment to achieve:
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name[len (name):] = List ("Tiger")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ', ', ' t ', ' I ', ' g ', ' e ', ' R ']
At this point the small partners will think, we can directly use the operator "+", but also convenient:
>>> name = List ("Scott")
>>> pwd = list ("Tiger")
>>> name + pwd
[' s ', ' C ', ' o ', ' t ', ' t ', ', ' t ', ' I ', ' g ', ' e ', ' R ']
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
From the output of these three modes of operation, you can see:
Extend and shard assignment are all modifications to the original list, relatively speaking, extend is more readable, while the operator "+" is to generate a new list, without affecting the original list, if
We need to generate a new list without affecting the original list, so we can use the operator "+".
4.Index
Find a value from the list the first (note is the first) match index position
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.index (' t ') # #第一个字母t的索引位置是3
>>> Name.index (' a ')
Traceback (most recent):
File "", Line 1, in?
ValueError:list.index (x): X not in List
>>> ' a ' in name
False
>>> ' A ' not in name
True
As you can see from the output, index is looking for the index position of the first match, and if the found element is not in the list, an error will be reported (return-1 will be better?), of course, if you want to avoid
Wrong, we can first use in operation to determine whether an element is in a list, if so, then the index operation.
5. Insert
Used to insert an object into the list, two parameters, the first being the index position, and the second inserted element object.
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name.insert (2, ' Tiger ') # #在索引为2的地方插入字符串tiger
>>> Name
[' s ', ' C ', ' Tiger ', ' o ', ' t ', ' t ']
We can also assign values with shards:
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name[2:2] = [' Tiger ']
>>> Name
[' s ', ' C ', ' Tiger ', ' o ', ' t ', ' t ']
>>> Name[2:2] = ' Tiger '
>>> Name
[' s ', ' C ', ' t ', ' I ', ' g ', ' e ', ' r ', ' Tiger ', ' o ', ' t ', ' t ']
It is important to note that if you insert an element, you need to enclose it in [], or, if you use a string directly, it is the list of inserted strings, which is added after the index position.
Of course, the readability of the insert with the score slice is strongly assigned.
6. Pop
Removes an element from the list (the last element) and returns the value of the element
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name.pop ()
' t '
>>> Name
[' s ', ' C ', ' o ', ' t ']
>>> name.append ("T")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
Shard Assignment simulates pop:
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name[len (name)-1:] = []
>>> Name
[' s ', ' C ', ' o ', ' t ']
This simulates the Stack's FIFO FIFO with pop and append.
7. Remove
Remove the first occurrence of a value in the list: if there are two equal elements, just remove a matching element, if an element does not exist in a list, it will be an error, and can only be
Removes a single element.
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> name.remove ("T") #去掉第一个t
>>> Name
[' s ', ' C ', ' o ', ' t ']
>>> name.remove ("A") #不存在会报错
Traceback (most recent):
File "", Line 1, in?
ValueError:list.remove (x): X not in List
>>> "A" not in name
True
>>> Name.remove ("s", "C") #一次只能移除一个元素
Traceback (most recent):
File "", Line 1, in?
Typeerror:remove () takes exactly one argument (2 given)
8.Revense
Reverse the elements in the list
>>> name = List ("Scott")
>>> Name
[' s ', ' C ', ' o ', ' t ', ' t ']
>>> Name.reverse ()
>>> Name
[' t ', ' t ', ' o ', ' C ', ' s ']
9. Sort & Sorted
The sort method is used to sort the list, modify the original list, and not return a sorted copy of the list
>>> result = [8,5,5,3,9]
>>> Result.sort ()
>>> result
[3, 5, 5, 8, 9]
If we want to return a sorted copy of the list without affecting the original list, one way, we can first assign the original list (which can be copied with a shard), and then
The other way to do sort on a replicated list is to use the sorted function, which returns a sorted list copy:
>>> result = [8,5,5,3,9]
>>> result2 = sorted (Result)
>>> result
[8, 5, 5, 3, 9]
>>> RESULT2
[3, 5, 5, 8, 9]