Home > Article > Backend Development > What are the basic operations on python lists?
The basic operations of python lists are: 1. Create a list, just enclose different data items separated by commas in square brackets; 2. Add new elements; 3. Traverse the list; 4. Access the list The value; 5. Delete elements from the list.
Related free learning recommendations: python tutorial( Video)
The basic operations of python lists are:
Mainly introduces the detailed operation methods of lists (List) in Python, including creating, For access, update, delete, other operations, etc., friends in need can refer to it.
1. Create a list. Just enclose the different data items separated by commas in square brackets
List = ['wade','james','bosh','haslem']
Like the index of the string, the list index starts from 0. The list can be intercepted, combined, etc.
2. Add new elements
1 List.append('allen') #方式一:向list结尾添加 参数object 2 >>> a=[1,2,3,4] 3 >>> a.append(5) 4 >>> print(a) 5 [1, 2, 3, 4, 5] 6 7 List.insert(4,'lewis') #方式二:插入一个元素 参数一:index位置 参数二:object 8 >>> a=[1,2,4] 9 >>> a.insert(2,3) 10 >>> print(a) 11 [1, 2, 3, 4] 12 13 List.extend(tableList) #方式三:扩展列表,参数:iterable参数 14 >>> a=[1,2,3] 15 >>> b=[4,5,6] 16 >>> a.extend(b) 17 >>> print(a) 18 [1, 2, 3, 4, 5, 6]
3. Traverse the list
for i in List: print i,
4. Access the values in the list
Use subscript index to access the values in the list. You can also use square brackets to intercept characters, as shown below:
>>> List = [1, 2, 3, 4, 5, 6, 7 ] >>> print(List[3]) 4
5. Delete elements from list
1 List.remove() #删除方式一:参数object 如有重复元素,只会删除最靠前的 2 >>> a=[1,2,3] 3 >>> a.remove(2) 4 >>> print(a) 5 [1, 3] 6 7 List.pop() #删除方式二:pop 可选参数index删除指定位置的元素 默认为最后一个元素 8 >>> a=[1, 2, 3, 4, 5, 6] 9 >>> a.pop() 10 6 11 >>> print(a) 12 [1, 2, 3, 4, 5] 13 14 15 del List #删除方式三:可以删除整个列表或指定元素或者列表切片,list删除后无法访问。 16 >>> a=[1, 2, 3, 4, 5, 6] 17 >>> del a[5] 18 >>> print(a) 19 [1, 2, 3, 4, 5] 20 >>> del a 21 >>> print(a) 22 Traceback (most recent call last): 23 File "<pyshell#93>", line 1, in <module> 24 print(a)
6. Sort and reverse code
1 List.reverse() 2 >>> a=[1, 2, 3, 4, 5, 6] 3 >>> a.reverse() 4 >>> print(a) 5 [6, 5, 4, 3, 2, 1] 6 7 8 List.sort() #sort有三个默认参数 cmp=None,key=None,reverse=False 因此可以制定排序参数 9 >>> a=[2,4,6,7,3,1,5] 10 >>> a.sort() 11 >>> print(a) 12 [1, 2, 3, 4, 5, 6, 7] 13 #python3X中,不能将数字和字符一起排序,会出现此报错 14 >>> a=[2,4,6,7,3,1,5,'a'] 15 >>> a.sort() 16 Traceback (most recent call last): 17 File "<pyshell#104>", line 1, in <module> 18 a.sort() 19 TypeError: unorderable types: str() < int()
7. Python list interception
Python’s list interception has the same type as string operation, as shown below:
L = ['spam', 'Spam', 'SPAM!'] 操作: Python 表达式 结果 描述 L[2] 'SPAM!' 读取列表中第三个元素 L[-2] 'Spam' 读取列表中倒数第二个元素 L[1:] ['Spam', 'SPAM!'] 从第二个元素开始截取列表
8. Python list operation functions and methods
List operations include the following functions:
1. cmp(list1, list2): compare elements of two lists (python3 has been discarded)
2. len(list): list Number of elements
3. max(list): Returns the maximum value of list elements
4. min(list): Returns the minimum value of list elements
5. list(seq ): Convert tuple to list
1 列表操作常用操作包含以下方法: 2 1、list.append(obj):在列表末尾添加新的对象 3 2、list.count(obj):统计某个元素在列表中出现的次数 4 3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) 5 4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置 6 5、list.insert(index, obj):将对象插入列表 7 6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 8 7、list.remove(obj):移除列表中某个值的第一个匹配项 9 8、list.reverse():反向列表中元素 10 9、list.sort([func]):对原列表进行排序
The above is the detailed content of What are the basic operations on python lists?. For more information, please follow other related articles on the PHP Chinese website!