标签:c style class blog code java
Python一维数组初始化:
>>> list=[] >>> type(list) <type ‘list‘> >>> list []
Python二维数组初始化:
>>> lists = [[]] * 3 >>> lists [[], [], []] >>> lists[0].append(3) >>> lists [[3], [3], [3]]
"[[]]" 是一个包含了空list的list,3个“[[]]”指向了同一个空‘lists’。修改其中一个,会导致其他的元素都会被修改。
另外一种初始化方式:
>>> lists = [[] for i in range(3)] >>> lists[0].append(3) >>> lists[1].append(5) >>> lists[2].append(7) >>> lists [[3], [5], [7]]
或者:
>>> multilist = [[0 for col in range(3)] for row in range(3)] >>> multilist [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
动态创建和初始化数组:
>>>list=[] >>>multilist=[] >>>for i in range(9): ... list.append(i+1) >>>list [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for i in range(3): ... multilist.append(list[3*i:3*(i+1)]) >>> multilist [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> multilist[1][1] 5
>>> len(multilist)
3
>>> multilist.append([0 for col in range(4)])
>>> len(multilist)
4
>>> multilist
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 0, 0, 0]]
标签:c style class blog code java
原文地址:http://www.cnblogs.com/sunkylin/p/3774576.html