【发布时间】:2014-12-29 08:05:23
【问题描述】:
我正在尝试实现此功能,但我不确定具体如何实现。我知道我们必须使用 for 循环来解决这个问题,但与设置变量等一样,或者它是否包含嵌套的 for 循环,我不确定。
def reshape(thelist, rows, cols):
"""Returns: A rows*cols 2D list with the contents of thelist
Hint: In a 2D list, the element at row x and col y is the
x*cols+y element listed.
Example: reshape([1,2,3,4],2,2) returns [[1,2], [3,4]]
Example: reshape([1,2,3,4,5,6],3,2) returns [[1,2], [3,4], [5,6]]
Example: reshape([1,2,3,4,5,6],2,3) returns [[1,2,3] ,[4,5,6]]
Precondition: thelist is a list of numbers of size rows*cols. rows
and cols are positive integers."""
【问题讨论】:
-
你为什么不尝试一种方法并显示你有问题的地方呢?一个问题不仅有一个可能的答案。
标签: python for-loop multidimensional-array