Sunday 15 February 2015

List iterations Python -



List iterations Python -

im trying work on popular python question - rotating 2d list. given list = [[1,2,3,4],[5,6,7,8],[12,13,14,15]].

i know simple solution exists : zip(*list[::-1]. wanted create own function. go this:

def flip(list): output = [] temp = len(list) row in range(temp): newlist = [] col in range(temp): newlist.append(list[col][temp - row - 1]) print list[col][temp - row -1] output.append(newlist)

but works when have n*n matrix. should alter if have am*n matrix . doing wrong here

you using temp = len(list) upper boundary both iterations. m×n matrix of course of study requires different boundaries each.

if assume each inner list in outer list has same length, save both lengths first , iterate sizes then:

m = len(list) n = len(list[0])

python

No comments:

Post a Comment