Python list simple application

List

List definition

The list is an ordered and modifiable collection

function

1. The append() function: means adding a new object in the list,
such as:

list=['a','b','c','4']
list.append('e')
print(list)
Run:
['a', 'b', 'c', '4', 'e']

2. Count(obj) function: Find the number of occurrences of obj in the list.

list=['a','b','c','a']
print("a出现的次数:",list.count('a'))
print("b出现的次数:",list.count('b'))
print("d出现的次数:",list.count('d'))
Run:
a出现的次数: 2
b出现的次数: 1
d出现的次数: 0

3. The extend() function: append multiple values ​​from another sequence at the end of the list at once. (List expansion)

list1=['a','b','c']
list2=['d','e','f']
list1.extend(list2)
print("合并后的列表:",list1)
Run:
合并后的列表: ['a', 'b', 'c', 'd', 'e', 'f']

4. Index() function: find a value from the list, the index position of the first matching item. (Find)

list=['a','b','c','d']
print(list.index('c'))
print(list.index('b'))
Run:
2
1

5. Insert() function: insert the object into the specified position in the list

list=['a','b','c','d']
list.insert(1,'f')
print("插入位置1一个f:",list)

Run:
插入位置1一个f: ['a', 'f', 'b', 'c', 'd']

6. The remove() function: Remove the first match of a value in the list.

list=['a','b','c','d','c']
list.remove('c')
print("移除后结果:d",list)
Run:
移除后结果:d ['a', 'b', 'd', 'c']

7. del function: delete the elements of the specified sequence.

list=['a','b','c','d','e']
del list[3]
print("删除后的列表:",list)
Run:
删除后的列表: ['a', 'b', 'c', 'e']

8. Pop function: Popping an element is the same as deleting an element, it removes an element item from the list.
9. The reverse() function: reverse list elements

list=['a','b','c','d','e']
list.reverse()
print("反向后列表:",list)
Run:
反向后列表: ['e', 'd', 'c', 'b', 'a']

10. List.sort() function: sort the original list.
Note: To sort the elements of the list, these elements must be of the same type, for example, all of them are strings, or all of them are numeric values, so that they can be compared in size. If the types are mixed, it cannot be done.
Let's write this first, continue to write an article, and learn the reverse engineering seriously in the future, let's put aside other things!

Guess you like

Origin blog.csdn.net/weixin_46148324/article/details/105202577