浅拷贝
上面的2张ppt的代码如下:
def maxItemLength(a):
maxLen = 0
rows = len(a)
cols = len(a[0])
for row in range(rows):
for col in range(cols):
maxLen = max(maxLen, len(str(a[row][col])))
return maxLen
# we want to write our own function # that prints 2d lists a bit nicer.
def print2dList(a):
if (a == []):
print([])
return
rows = len(a)
cols = len(a[0])
fieldWidth = maxItemLength(a)
print("[ ", end="")
for row in range(rows):
if (row > 0): print("\n ", end="")
print("[ ", end="")
for col in range(cols):
if (col > 0): print(", ", end="")
# The next 2 lines print a[row][col] with the given fieldWidth
formatSpec = "%" + str(fieldWidth) + "s"
print(formatSpec % str(a[row][col]), end="")
print(" ]", end="")
print("]")
# Let's give the new function a try!
a = [ [ 1, 2, 3 ] , [ 4, 5, 67 ] ]
print2dList(a)
Python类型分类
按对象是否能容纳多个子对象分类
原子类型:数值(所有数值类型)、字符串
容器类型:列表、元组、字典
按值是否能原地更新分类
不可变对象:数值、字符串、元组
可变对象:列表、字典
按数据访问方式分类
直接访问:数值
顺序访问:字符串、列表、元组
映射访问:字典