Tuesday, 6 August 2013

Weird python list syntax with an attribute

Weird python list syntax with an attribute

I'm currently learning python by writing filters for the MCedit python
program, a 3D block editor for Minecraft. Filters are small pieces of code
that can be ran inside the program, as a function.
One of the functions given by MCedit allows to exctract a schematic (here
named temp) to modify it. In the demo, the creator of the program modify
the block attribute of the schematic, by the following line :
temp.blocks[temp.blocks == 14] = 52
The .blocks attribute is made of concatenated lists, on 3 layers (x, y, z).
This piece of code replaces any of the value in any of the lists being
equal to 14 by 52.
I tried to make it change other values, with:
temp.blocks[temp.blocks == 14 or temp.blocks == 15] = 52
but it simply doesn't work.
I know there are other ways to do it, but this one being particularely
simple, compact and efficient, I'd like to understand how it works.

No comments:

Post a Comment