列表的加法,列表的乘法:重复列表元素
列表的嵌套:matrix = [[1,2,3],[4,5,6].[7,8,9]] 二维列表
访问嵌套列表
创建嵌套列表:
A = [0] *3
for i in range(3):
A[i] = [0] * 3 3*3
S = [[0]*3 for i in range(3)]
浅拷贝与深拷贝:浅拷贝只拷贝了外层对象,含有嵌套的拷贝,拷贝的只是其引用
浅拷贝:
x = [[1,2,3],[4,5,6].[7,8,9]]
y = x.copy 若后x值改变,y也随之改变
法二:import copy
x = [[1,2,3],[4,5,6].[7,8,9]]
y = copy.copy(x)
深拷贝:
x = [[1,2,3],[4,5,6].[7,8,9]]
y = copy.deepcopy(x)
列表推导式:
oho = [1,2,3,4,5]
oho = [i*2 for i in oho] oho [2,4,6,8,10]
x = [i+1 for i in range(10)] x值为1-9
y = [c*2 for c in "FishC"] y最终为 ['FF','ii','ss','hh','cc']
将单一字符串转换为对应的编码 ord
code = [ord(c) for c in "FishC"] code结果为[70,105,115,104,67]
注:列表推导式拓展
even = [i+1 for i in range(10) if i % 2 == 0] even [1,3,5,7,9]
words = ["Great","FishC","Brilliant","Excellent","Fantastic"]
fwords = [w for w in words if w[0]=='F'] 筛选F开头的单词 列表7
[x + y for x in "fishc" for y in "FISHC"] //'fF' 'iI' ···
标签:oho,列表,嵌套,range,拷贝,copy,Python3 From: https://www.cnblogs.com/cspecialr/p/17129403.html