list - Creating a 'grid' of instanecs of a class in Python -
i've been using site lot finding answers various programming questions, , firstly want thank tons of you've been throughout journey of programming. anyway, off question:
i want create list of lists of instances of class. wow, mouthful. let me try explain better in code. example, following 3x3 'grid':
foo = [bar(), bar(), bar()], bar(), bar(), bar()], bar(), bar(), bar()]]
which works perfectly, however, don't know dimensions of 'grid' beforehand. in mind, logical attempt following:
foo = [[bar()]*num]*num
however, not work properly. believe error python populating 'grid' full of same instance of bar(), problematic. there way 'nice' can complete task of filling grid different instances of bar()?
foo = [[bar() _ in xrange(num)] _ in xrange(num)]
sequence multiplication makes big grid of references same object. need use list comprehensions evaluate bar()
expression repeatedly.
Comments
Post a Comment