用len()方法,统计全部元素的个数。
用count()方法,统计指定值的元素的个数。
用max()方法,统计元素中的最大值(要求元素类型相同;数字类型直接比较,其它类型比较id)
用min()方法,统计元素中的最小值(要求元素类型相同;数字类型直接比较,其它类型比较id)
用index()方法,查找指定值的元素的索引位置(第一个匹配项)。
用reverse()方法,翻转列表中的元素。
用copy()方法,浅拷贝并生成新的列表。
用deepcopy()方法,深拷贝并生成新的列表。
用sort()方法,在原列表基础上进行排序。
用sorted()方法,将新列表基础上对原列表的元素进行排序。
list_1 = [2018, 10, '2018-10-1', ['hi', 1, 2], (33, 44)]
len(list_1) == 5
list_1.count(10) == 1 # 元素10的数量为1
list_1.index(10) == 1 # 元素10的索引为1
list_1.reverse() # list_1 == [(33, 44), ['hi', 1, 2], '2018-10-1', 10, 2018]
# 比较浅拷贝与深拷贝
import copy
list_a = [2018, 10, '2018-10-1', ['hi', 1, 2], (33, 44)]
list_b = ['hi', 1, 2]
list_c = list_a.copy() # list_c == [2018, 10, '2018-10-1', ['hi', 1, 2], (33, 44)]
list_d = copy.deepcopy(list_a) # list_d == [2018, 10, '2018-10-1', ['hi', 1, 2], (33, 44)]
# 改变原列表中的可变对象元素
list_a[3].append('new') # list_a == [2018, 10, '2018-10-1', ['hi', 1, 2, 'new'], (33, 44)]
# 浅拷贝中的可变对象会随原列表变化而变化
list_c == [2018, 10, '2018-10-1', ['hi', 1, 2, 'new'], (33, 44)]
# 深拷贝中的可变对象不会随原列表变化而变化
list_d == [2018, 10, '2018-10-1', ['hi', 1, 2], (33, 44)]
# 比较sort() 与 sorted()
list_1 = list_2 = [2,1,4,6,5,3]
list_1.sort() # 原列表变化:list_1 == [1,2,3,4,5,6]
list_3 = sorted(list_2) # 原列表不变:list_2 == [2,1,4,6,5,3]; list_3 == [1,2,3,4,5,6]
比较喜欢深copy、浅copy和sort、sorted感觉面试必考