Home > Backend Development > Python Tutorial > Detailed introduction to list operations in python (example)

Detailed introduction to list operations in python (example)

不言
Release: 2018-09-20 15:43:56
Original
2602 people have browsed it

This article brings you a detailed introduction (example) about list operations in python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Lest (list):

Definition and creation of lists:

List: is one of the most commonly used data structures in python and other languages. Python uses [] to parse lists
Lists are mutable. --You can change the contents of the list
You can use slices

a=['张三','李四','王五','赵六']
Copy after login

To add, delete, and modify the list:

1: Check ([])

a=['张三','李四','王五','赵六']
print(a[1:3])#左边取到,右边取不到
print(a[-1])
print(a[3:1:-1])
>>['李四', '王五']
>>赵六
>>['赵六', '王五']
Copy after login

2: Add (append, insert)
append can only insert the object to the end of the list
insert can specify the position where the object is inserted

a=['张三','李四','王五','赵六']
a.append('chen')
a.insert(2,'zheng')
print(a)
>>['张三', '李四', 'zheng', '王五', '赵六', 'chen']
Copy after login

3: Change (reassign)

a=['张三','李四','王五','赵六']
a[2]='刘六'
a[0:2]=['chen','zheng']
print(a)
>>['chen', 'zheng', '刘六', '赵六']
Copy after login

4: Delete (remove, del, pop)

a=['张三','李四','王五','赵六']
a.remove('王五')
del a[1]
print(a.pop(1))   #pop是有一个返回值的
print(a)
>>赵六
>>['张三']
Copy after login

Common operations:

#count counts the number of times an element appears in the list

a=['to','too','or','not','to'].count('to')
print(a)
>>2
Copy after login

#extend appends multiple values ​​from another list to the end of the list at once.

a=['to','too','or','not','to']
b=[1,2,3,4,5]
a.extend(b)
print(a)
>>['to', 'too', 'or', 'not', 'to', 1, 2, 3, 4, 5]
Copy after login

#index is used to find the index position of the first matching item of a value from the list

a=['张三','李四','王五','赵六']
print(a.index('王五'))
>>2
Copy after login

#reverse stores the list in reverse

a=['张三','李四','王五','赵六']
a.reverse()
print(a)
>>['赵六', '王五', '李四', '张三']
Copy after login

#sort is used to sort the list at the original position

a=[4,3,63,25,46,54]
a.sort()
print(a)
>>[3, 4, 25, 46, 54, 63]
Copy after login

1. Shallow copy can only copy the outermost layer. If the inner layer is modified, both the original list and the new list will change.

2. Deep copy refers to completely cloning the original list into a new one.

The above is the detailed content of Detailed introduction to list operations in python (example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template