[Tutor] Question about lists

Peter Otten __peter__ at web.de
Fri Sep 21 16:04:47 CEST 2012


Leo Degon wrote:

> I'm trying to create a class where the main focus is creating a list whose
> elements are lists and the elements of those lists are collection of zeros
> and ones. I am trying to create functions to rotate the list ninety
> degrees, to reflect it. Having a few problems with the rotation.
> get TypeError: 'list' object is not callable
> 
> def pset(n):
>     for i in n:
>         print(i)
> class board():
>     def make(self,size):
>         b=[]
>         for i in range(size[0]):
>             b.append([])
>             for j in range(size[1]):
>                 b[i].append(0)
>         return b
> 
>     def rotate(self,board,size):

The local parameter "board" shades the class with the same name, 

>         size[0],size[1]=size[1],size[0]
>         new=board(size)

so here you are not making a new board instance but instead trying to call 
the board parameter which is a list. To fix this problem you have to rename 
the parameter or the class. I recommend that you rename the class to Board 
(by convention class names start with an uppercase letter).

As to the parameter board, you actually don't need it at all -- you can 
instead make the board attribute of the new Board instance a rotated version 
of self.board.

(There are more bugs in your code, but I suggest you tackle them one at a 
time. Come back here if you need help fixing them)

>         lists=[]
>         for j in range(size[1]):
>             lists.append([])
>             for i in range(size[0]).__reversed__():
>                 lists[j].append(board[i][j])
>         for i in range(size[1]):
>             for j in range(size[0]):
>                 new.board[i,j]=lists[i,j]
>         return(new.board)
>     def __init__(self,size):
>         self.size=size
>         self.board=self.make(size)
> y=[7,7]
> x=board(y)
> pset(x.board)
> x.board[0][0]=1
> print()
> pset(x.board)
> print()
> x.board=x.rotate(x.board,x.size)
> pset(x.board)
> print()




More information about the Tutor mailing list