1

I'm trying to implement this function but I'm not sure exactly how to do so. I know we have to use a for loop for this problem but as with setting with variables and such, or if it contains a nested for loop, I am unsure.

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."""
jfs
  • 399,953
  • 195
  • 994
  • 1,670
Steve
  • 65
  • 1
  • 6
  • Why don't you try one way and show where you have a problem instead? There isn't only one possible answer for a question. – fredtantini Dec 29 '14 at 08:09

4 Answers4

1
def reshape(thelist, rows,cols):  
    parent_list = []
    count = 0
    for j in range(rows):
        new_list = []
        for i in range(cols):
            new_list.append(thelist[count])
            count+=1
        parent_list.append(new_list)
    return parent_list
ZdaR
  • 22,343
  • 7
  • 66
  • 87
  • This can be done without nesting loops but this method is more clear and easy to understand for a beginner. – ZdaR Dec 29 '14 at 08:14
1

See What is the most “pythonic” way to iterate over a list in chunks?:

def reshape(thelist, rows,cols):  
    return [thelist[i:i + cols] for i in range(0, len(thelist), cols)]

If you don't need to preserve types, you could also write it as zip(*[iter(thelist)]*cols). See the explanation in the linked question.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

The simplest way I could think of is this:

def thesplit(input_list, rows, cols):
    output_list = []
    for i in range(rows):
        pos = i*cols
        output_list.append(input_list[pos:pos+cols])
    return output_list

If you do not want the variable pos, you can use this instead:

output_list.append(input_list[(i*pos):((i+1)*pos)])

which is same as that of the previous sample.

thiruvenkadam
  • 4,170
  • 4
  • 27
  • 26
0

This is just an expansion of J.F. Sebastian's 2nd solution:

def reshape(seq, rows, cols):
    return [list(u) for u in zip(*[iter(seq)] * cols)]

rows, cols = 3, 4
seq = range(rows * cols)
print reshape(seq, rows, cols)

output

[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]

I initially found this algorithm a little puzzling. The key to understanding how it works is to realize that [iter(seq)] * cols creates a list of cols references to the same iterator not cols independent iterators. So each time zip pulls a value from one of the entries it effectively gets the next entry from the original list. And so another way to write essentially the same algorithm is:

def reshape(seq, rows, cols):
    g = iter(seq)
    return [[g.next() for j in range(cols)] for i in range(rows)]
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182