Ian Clark wrote: > On 4/30/07, Casey Duncan <[EMAIL PROTECTED]> wrote: >> passible_at = [[True] * 100] * 100 # Create a 2 dimensional list with >> an element for each tile > > This does not do what you think it would: > > Someone more skilled than I can explain *why* the first one does what > it does. I just know one way to get around it (and I'm sure there's a > more elegant solution).
The expression [True]*100 evaluates to a list that has 100 elements, each pointing to the same thing (the True object). Therefore, the expression [[True]*100]*100 evaluates to a list that has 100 elements, each pointing to the same thing (the first list). This may be easier to explain with the following example: >>> l = [{}]*3 >>> l [{}, {}, {}] >>> l[0]['a'] = 1 >>> l [{'a': 1}, {'a': 1}, {'a': 1}] Multiplying a list by a number creates more pointers to the same objects; if those objects are mutable, and they are changed, the pointers reflect the new, changed, object. Your proposed workaround, which uses a repeated call to append, creates a new element each time through the loop, and so does not exhibit the same unpleasant behavior. Ultimately, though, Samuel Mankins's question is dependent on how he stores his blocks. The suggested data structures (a dict which maps location->block, or two-dimensional array of blocks) are both suitable (assuming you don't get bitten by gotchas like this one). Ethan
signature.asc
Description: OpenPGP digital signature