python基础复习 - 列表基本操作
Posted kaezah
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础复习 - 列表基本操作相关的知识,希望对你有一定的参考价值。
1 class list(object): 2 """ 3 Built-in mutable sequence. 4 5 If no argument is given, the constructor creates a new empty list. 6 The argument must be an iterable if specified. 7 """ 8 def append(self, *args, **kwargs): # real signature unknown 9 """ Append object to the end of the list. """ 10 pass 11 12 def clear(self, *args, **kwargs): # real signature unknown 13 """ Remove all items from list. """ 14 pass 15 16 def copy(self, *args, **kwargs): # real signature unknown 17 """ Return a shallow copy of the list. """ 18 pass 19 20 def count(self, *args, **kwargs): # real signature unknown 21 """ Return number of occurrences of value. """ 22 pass 23 24 def extend(self, *args, **kwargs): # real signature unknown 25 """ Extend list by appending elements from the iterable. """ 26 pass 27 28 def index(self, *args, **kwargs): # real signature unknown 29 """ 30 Return first index of value. 31 32 Raises ValueError if the value is not present. 33 """ 34 pass 35 36 def insert(self, *args, **kwargs): # real signature unknown 37 """ Insert object before index. """ 38 pass 39 40 def pop(self, *args, **kwargs): # real signature unknown 41 """ 42 Remove and return item at index (default last). 43 44 Raises IndexError if list is empty or index is out of range. 45 """ 46 pass 47 48 def remove(self, *args, **kwargs): # real signature unknown 49 """ 50 Remove first occurrence of value. 51 52 Raises ValueError if the value is not present. 53 """ 54 pass 55 56 def reverse(self, *args, **kwargs): # real signature unknown 57 """ Reverse *IN PLACE*. """ 58 pass 59 60 def sort(self, *args, **kwargs): # real signature unknown 61 """ Stable sort *IN PLACE*. """ 62 pass
插入
#插入 insert a = [i for i in range(3)] a.insert(1,4) print(a) #[0, 4, 1, 2]
#extend a = [0,1] a.extend([2,3]) print(a) #[0,1,2,3]
删除
#pop a = [3,4,5] deleted_element = a.pop(1) print(deleted_element) #4 print(a) #[3,5]
#pop默认删除最后一个元素 a = [1,2,3] deleted_item = a.pop() print(deleted_item) #3 print(a) #[1,2]
#remove 按元素删除 a = [1,2,2,3] a.remove(2) print(a) #[1,2,3]
#clear 清空 a = [1,2,3] a.clear() print(a) #[]
#del 删除 a = [1,2,3] del a print(a) #NameError: name ‘a‘ is not defined
#del 切片删除 a = [1,2,3,4] del a[1:] print(a) #[1]
将字符串转化为列表
#list a = ‘12‘ a = list(a) print(a) #[‘1‘,‘2‘] #切片 a = [] a[0:2] = ‘12‘ print(a) #[‘1‘,‘2‘] #以上就算序号超过字符串长度也没关系 a = [] a[0:3] = ‘12‘ print(a) #[‘1‘,‘2‘]
#字符串长度超过所给序号也没关系g a = [] a[0:2]=‘1234‘ print(a) #[‘1‘, ‘2‘, ‘3‘, ‘4‘]
#对于列表赋值也是一样 a = [] a[0:1] = [i for i in range(10)] print(a) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
公共方法
len, count
排序
#sorted 不改变原字符串顺序,sort改变原字符串顺序,其他一样 import random a = [i for i in range(10)] random.shuffle(a) print(a) #[7, 4, 5, 9, 0, 1, 2, 8, 6, 3] print(sorted(a)) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(a) #[7, 4, 5, 9, 0, 1, 2, 8, 6, 3] print(sorted(a, reverse = True)) #[9, 8, 7, 6, 5, 4, 3, 2, 1, 0] print(a.sort()) #None print(a) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] a.sort(reverse = True) print(a) #[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
反转
reverse a = [7, 4, 5, 9, 0, 1, 2, 8, 6, 3] a.reverse() print(a) #[3, 6, 8, 2, 1, 0, 9, 5, 4, 7]
以上是关于python基础复习 - 列表基本操作的主要内容,如果未能解决你的问题,请参考以下文章