1. Two methods of listing 1. Built-in methods for lists
Add to List
格式:x.__add__(y)等同于x+y例如:list1 = [1,2,3] print(list1.__add__([4,5,6])) >>> [1,2,3,4,5,6]返回值:list
Note: The preceding y can be list/tuple/str/dict/set and so on, but it cannot be int.
Whether it is an inclusive relationship
格式:x.__contains__(y)等同于y in x例如:list1 = [1,2,3] print(list1.__contains__([2,3])) >>> True返回值:bool
Note: The preceding y can be list/tuple/str/dict/set and so on, but it cannot be int.
Specify list index deletions (modify original list)
格式:x.__delitem__(index)等同于delete x[index]例如:list1 = [1,2,3] list1.__delitem__(2) print(list1) >>> [1,2]返回值:None
Determine if two lists are equal
格式:x.__eq__(y)等同于x==y例如:list1 = [1,2,3] print(list1.__eq__([2,3])) >>> False返回值:bool
Determine if list 1 is greater than or equal to List 2
格式:x.__ge__(y)等同于x>=y例如:list1 = [1,2,3] print(list1.__ge__([1,2])) >>> True返回值:bool
Note: This method is primarily judged by the first element of the list
Methods for accessing properties
格式:x.__getitem__(index)等同于x[index]例如:list1 = [1,2,3] print(list1.__getitem(2)) >>> 3返回值:object
Determine if list 1 is greater than List 2
格式:x.__gt__(y)等同于x>y例如:list1 = [1,2,3] print(list1.__gt__([2])) >>> False返回值:bool
Note: This method is primarily judged by the first element of the list
Add Value of Assignment
格式:x.__iadd__(y)等同于x += y例如:list1 = [1,2,3] print(list1__iadd__(‘3‘)) >>> [1, 2, 3, ‘3‘]返回值:list
Note: The preceding y can be list/tuple/str/dict/set, but not int, and the original list will change.
Multiply the assignment value
格式:x.__imul__(num)等同于x *= num例如:list1 = [1,2,3] print(list1.__imul__(2)) >>> [1,2,3,1,2,3]返回值:list
Note:num can only be of type int, and the original list will change.
Constructor method (Modify the original list)
格式:x.__init__(y)例如:list1 = [1,2,3] list1.__init__([4,5]) print(list1) >>> [4,5]返回值:list
Note:y does not specify to construct an empty list
Iterators
格式:x.__iter__()等同于iter(x)例如:list1 = [1,2,3] die = list1.__iter__() print(die.__next__()) >>> 1返回值:迭代器
Determine if list 1 is less than or equal to List 2
格式:x.__le__(y)等同于x<=y例如:list1 = [1,2,3] print(list1.__le__([2])) >>> True返回值:bool
Note: This method is primarily judged by the first element of the list
List length
格式:x.__len__()等同于len(x)例如:list1 = [1,2,3] print(list1.__len__()) >>> 3返回值:int
Determine if list 1 is less than List 2
格式:x.__lt__(y)等同于x<y例如:list1 = [1,2,3] print(list1.__lt__([2])) >>> True返回值:bool
Note: This method is primarily judged by the first element of the list
Multiplication of lists
格式:x.__mul__(num)等同于x*y例如:list1 = [1,2,3] print(list.__mul__(2)) >>> [1,2,3,1,2,3]返回值:list
Determine if two lists are not equal
格式:x.__ne__(y)等同于x!=y例如:list1 = [1,2,3] print(list1.__ne__([4,5])) >>> True返回值:bool
Redefining a list
格式:x.__new__(list)例如:list1 = [1,2,3] print(list1.__new__(list)) >>> []返回值:list
Built-in transformations
格式:x.__reversed__()等同于reversed(x)例如:list1 = [1,‘2‘,3,‘d‘,‘$‘] die = list1.__reversed__() print(die.__next__()) >>> ‘$‘返回值:迭代器
Right → Multiply left list
格式:x.__rmul__(num)等同于num*x例如:list1 = [1,2,3] print(list1.__rmul__(2)) >>> [1,2,3,1,2,3]返回值:list
Modify the elements of the specified index (modify the original list)
格式:x.__getitem__(index,value)等同于x[index]=value例如:list1 = [1,2,3] list1.__getitem__(1,5) print(list1) >>> [1,5,3]返回值:None
List in memory size, converted to bytes for calculation
格式:x.__sizeof__()例如:list1 = [1,2,3] list2 = [2,3] print(list1.__sizeof__()) >>> 64 print(list2.__sizeof__()) >>> 56 返回值:int#列表长度的不同,导致类别在内存中的大小也不一样。
2. Common methods
Add an element to the original list
格式:x.append(y) #直接修改原列表,添加的元素可以任意对象,每次只能添加一个元素例如:list1 = [1,2,3,4] list1.append([1,2,3]) print(list1) >>> [1,2,3,4,[1,2,3]]返回值:None
Clear the original list
格式:x.clear() #删除列表中所有元素,列表本身不删除例如:list1 = [1,2,3,4] list1.clear() print(list1) >>> []返回值:None
Copy List
格式:x.copy() #复制原列表,生成一个新列表例如:list1 = [1,2,3,4] print(list1.copy()) >>> [1,2,3,4]返回值:list
Count
格式:x.count(value) #统计列表中指定元素的个数例如:list1 = [1,2,2,3,4,2,3] print(list1.count(2)) >>> 3返回值:int
Expand list
格式:x.extend() #在原列表中扩充列表(修改原列表),两个列表合并为一个列表例如:list1 = [1,2,3,4] list1.extend([‘1‘,‘2‘]) print(list1) >>> [1, 2, 3, 4, ‘1‘, ‘2‘]返回值:None
Removes the index of the specified element value
格式:x.index(value,strat,end) #在指定的索引范围内,查找元素的索引,默认全列表查找例如:list1 = [1, 2, 3, 4, ‘1‘, ‘2‘] print(list1.index(‘1‘,2,5)) >>> 4返回值:int
inserting elements
格式:x.insert(index,object) #在原列表中,指定索引位置处添加一个元素,元素可以是任意对象例如:list1 = [1, 2, 3, 4, ‘1‘, ‘2‘] list1.insert(3,‘four‘) print(list1) >>> [1, 2, 3, ‘four‘, 4, ‘1‘, ‘2‘] 返回值:None
Remove the value of the specified index
格式:x.pop(index) #删除指定索引的值,默认从列表的最后面开始删除例如:list1 = [1, 2, 3, ‘four‘, 4, ‘1‘, ‘2‘] print(list1.pop()) >>> ‘2‘ print(list1.pop(2)) >>> 3 print(list1) >>> [1, 2, ‘four‘, 4, ‘1‘]返回值:object
To remove the specified element
格式:x.remove(value) #删除指定元素,直接修改原列表例如:list1 = [1, 2, 3, ‘four‘, 4, ‘1‘, ‘2‘] list1.remove(3) print(list1) >>> [1, 2, ‘four‘, 4, ‘1‘, ‘2‘]返回值:None
Reverse
格式:x.reverse() #对列表进行升序排序后再进行颠倒例如:list1 = [1, 2, 3, ‘four‘, 4, ‘1‘, ‘2‘] list1.reverse() print(list1) >>> [‘2‘, ‘1‘, 4, ‘four‘, 3, 2, 1]返回值:None
Sort Ascending
格式:x.sort() #对列表进行升序排序,一般int和str类型不能同时存在例如:list1 = [6,2,3,4] list1.sort() print(list1) >>> [2,3,4,6]返回值:None
2. Operation of the list 1. The underlying operation of the list