代码之家  ›  专栏  ›  技术社区  ›  Traveling_Monk

python在控制结构中的应用

  •  1
  • Traveling_Monk  · 技术社区  · 16 年前

    我是一名php程序员,试图理解python的语法 我有基本的 for in

    for i in range(0,5):
    

    在php中是

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

    for x, y in z:
    

    那么到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(tileContainerName=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
    
    7 回复  |  直到 16 年前
        1
  •  1
  •   mtvee    16 年前

    self._v_scaleInfo: [(x,y),(x,y),...] 所以 for width, height in self._v_scaleInfo: 循环使用元组值填充数组的宽度和高度。

    php将类似于:

    $scaleInfo = array(array(x,y), array(x,y),...);
    
    for( $i = 0; $i < count($scaleInfo); $i++ ) {
      $width = $scaleInfo[$i][0];
      $height = $scaleInfo[$i][1];
      ...
    }
    
        2
  •  1
  •   inklesspen    16 年前

    for x,y in z ,z将是坐标对的列表,如 [(0,1), (2,5), (4,3)] . 通过for循环的每一圈 x 变量获取对中的第一个坐标,然后 y

        3
  •  0
  •   Robert Christie    16 年前

    在python中,可以有多个返回值。您还可以这样定义一个元组

    t = (1,2,3)
    

    那么a的值为1,以此类推。

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

    这会产生以下结果

    1 2
    3 4
    5 6
    
        4
  •  0
  •   ikkebr    16 年前

    假设z是Python中的元组列表。

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

    可能是这样的:

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

    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
    

    所以翻译

    for x, y in z:
    

    进入PHP将类似于:

    for ($i=0; $i < count($z); $i++){
        $x = $z[$i][0];
        $y = $z[$i][1];
    
        5
  •  0
  •   Perpetualcoder    16 年前

    for x,y in z
    

    实际上是使用枚举器(迭代器模式的特定于语言的实现)进行迭代,因为循环基于基于索引的迭代。

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

    注意,这将适用于python中的元组。

        6
  •  0
  •   abyx    16 年前

    它大致相当于(伪代码):

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

    这意味着 z 包含2个元素(例如,每个项目都是包含2个项目的列表)。

        7
  •  0
  •   Tendayi Mawushe    16 年前

    此结构允许您在多维集合上迭代,因此对于3x2列表,您可以有:

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

    这张照片是:

    1 2
    3 4
    5 6
    

    同样的结构也可用于某种意义上也是二维集合的词典:

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

    这张照片是:

    1 one
    2 two
    3 three
    

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

    1 2 3
    4 5 6
    

    在PHP中,我认为必须使用nest for循环来实现这一点,我不认为有一种构造可以实现Python中可能的多维列表解构。

    推荐文章