class Solution(object):
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
res=[[None]*c for i in range(r)]
if len(nums)*len(nums[0]) != r*c:
return nums
else:
for i in range(len(nums)*len(nums[0])):
res[i/c][i%c] = nums[i/len(nums[0])][i%len(nums[0])]
return res
- 关于二维列表的初始化
res=[[None]*c for i in range(r)]
,c是列数,i是行数,这里的i无实际意义,res=[[None]*c for _ in range(r)]