开发者

python for in control structure

开发者 https://www.devze.com 2022-12-12 19:26 出处:网络
I am a php programmer trying to understand python\'s for in syntax I get the basic for in for i in range(0,5):

I am a php programmer trying to understand python's for in syntax I get the basic for in

for i in range(0,5):

in php would be

for ($i = 0; $i < 5; $i++){

but what does this do

for x, y in z:

and what would be the translation to php?

This is the full code i am translating to php:

 def preProcess(self):
    """ plan for the arrangement of the tile groups """

    tier = 0
    tileGroupNumber = 0
    numberOfTiles = 0
    for width, height in self._v_scaleInfo:

      #cycle through columns, then rows
      row, column = (0,0)
      ul_x, ul_y, lr_x, lr_y = (0,0,0,0)  #final crop coordinates
      while not ((lr_x == width) and (lr_y == height)):

        tileFileName = self.getTileFileName(tier, column, row)
        tileContainerName = self.getNewTileContainerName(tileGroupNumber=tileGroupNumber)
        if numberOfTiles ==0:
          self.createTileContainer(tileContainerName=tileContainerName)
        elif (numberOfTiles % self.tileSize) == 0:
          tileGroupNumber += 1
          tileContainerName = self.getNewTileContainerName(tileGroupNumber=tileGroupNumber)
          self.createTileContainer(tileCon开发者_如何学CtainerName=tileContainerName)
        self._v_tileGroupMappings[tileFileName] = tileContainerName
        numberOfTiles += 1

        # for the next tile, set lower right cropping point
        if (ul_x + self.tileSize) < width:
          lr_x = ul_x + self.tileSize
        else:
          lr_x = width

        if (ul_y + self.tileSize) < height:
          lr_y = ul_y + self.tileSize
        else:
          lr_y = height

        # for the next tile, set upper left cropping point
        if (lr_x == width):
          ul_x=0
          ul_y = lr_y
          column = 0
          row += 1
        else:
          ul_x = lr_x
          column += 1

      tier += 1


self._v_scaleInfo: is an array of tuples, presumably, like [(x,y),(x,y),...] so for width, height in self._v_scaleInfo: loops through the array filling width and height with the tuple values.

php would go something like:

$scaleInfo = array(array(x,y), array(x,y),...);

for( $i = 0; $i < count($scaleInfo); $i++ ) {
  $width = $scaleInfo[$i][0];
  $height = $scaleInfo[$i][1];
  ...
}


In your simple example for x,y in z, z would be a list of coordinate pairs, like [(0,1), (2,5), (4,3)]. With each turn through the for loop, the x variable gets the first coordinate in the pair and y gets the second.


In python, you can have multiple return values. You can also define a tuple like this

t = (1,2,3)

To get access to the elements in t you can do the following:

a, b, c = t

Then a has the value 1, etc.

If you had an array of 2 element tuples, you could enumerate through them using the following code

z = [(1, 2), (3, 4), (5, 6)]
for x, y in z:
   print x, y

which produces the following

1 2
3 4
5 6


Suppose z is a list of tuples in Python.

Z = [(1,2), (1,3), (2,3), (2,4)]

it would be something like:

$z = array(array(1,2), array(1,3), array(2,3), array(2,4));

using that for x,y in z would result in:

z = [(1,2), (1,3), (2,3), (2,4)]
for x, y in z:
    print "%i %i" % (x,y)


1 2
1 3
2 3
2 4

so Translating

for x, y in z:

into PHP would be something like:

for ($i=0; $i < count($z); $i++){
    $x = $z[$i][0];
    $y = $z[$i][1];


Conceptually

for x,y in z

is actually iterating using an enumerator (language specific implementation of an iterator pattern), for loops are based on index based iteration.

for x,y in z would semantically be like

for (x=0 ; x<z.length ; x++ )
     for (y=1; x<z.length;y++) print z[x],z[y]

note this will work for tuples in python.


It's roughly equivalent to (pseudo code):

For every item i in z:
    x = i[0]
    y = i[1]
    Loop body happens here

It means that every item in z contains 2 elements (for example, every item is a list with 2 items).


This construct allows you to iterate over multi-dimensional collections so for a 3x2 list you could have have:

z = [[1,2], [3,4], [5,6]]
for x, y in z:
    print x, y

This prints:

1 2
3 4
5 6

The same construct could be used on a dictionary which is some sense also a 2-dimensional collection:

z = {1:"one", 2:"two", 3:"three"}
for x, y in z.items():
    for x, y in z.items():
        print x, y

This prints:

1 one
2 two
3 three

In Python this construct is general and work at any dimension, changing our original 3x2 list to a 2x3 list we could do this:

z = [[1,2,3], [4,5,6]]
for w, x, y in z:
    print w, x, y

This prints:

1 2 3
4 5 6

In PHP I think you have to do this with nest for loops, I do not think there is a construct to do the sort of multiple dimension list deconstruction that is possible in Python.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号