[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pygame] python newbie question re. arrays of arrays



Hi there

Trying to speedup pyplace... one thing I'd like to do is :
      blitbits = [ [] ] * MAPF_MASK_HEIGHT
To create an list of lists which I can then update,


Unfortunately it doesn't work, 'cos only one sublist gets created and
then duplicated

If you try this interactively :
>>>a = [ [], [], [], [], [] ]
>>>a
[[], [], [], [], []]
>>>a[3].append( 'fred')
>>>a
[ [], [], [], ['fred'], [] ]
                #Which is hopefully what you expected


However if you try :
>>>a = [ [] ] * 5
>>>a[3].append('fred')
>>>a
[[], [], [], [], []]
>>>a[3].append('fred')
>>>a
[['fred'], ['fred'], ['fred'], ['fred'], ['fred']]
                # Ugh

Now I see how it's happening... but...
I do question why, given that [] is empty you'd have to be a bit wierd
to want the DUP operator (*) to be
creating a list of references to the same array


QUESTION
Is there any syntactical sugar I can use to force * to create a new
empty list for each element ????

I've tried looking in "Programming Python" and a bit online.
I can just force the code to do what I want for now with the rather ugly

    blitbits = []
    for bb in range(MAPF_MASK_HEIGHT):
        blitbits.append([])


SUPPLEMENTARY QUESTION (bonus point!)
Can anyone direct me to a good Python website for experienced
programmers, i.e. I know C++/Java/Assembler/Perl/Pascal
but need to know the syntax wierdities of Python.

(programming with Pygame is an excellent 'rewarding' environment to
learn Python - BTW)




____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org