【问题标题】:Python: returning the cell coordinatesPython:返回单元格坐标
【发布时间】:2014-08-04 10:31:22
【问题描述】:

我一直坚持编写一个函数,该函数接受一个大小的整数值,该值将返回所有单元格坐标。

例如:如果大小为 1,那么它将返回 (0,0)。如果大小为2,则返回(0,0), (0,1), (1,0), (1,1)

这是我到目前为止所做的

def get_cells(size):

    for x_axis in range(0, size):
        x = x_axis
        for y_axis in range(0, size):
            y = y_axis
    return (x, y)

此代码仅返回最后一个单元格坐标...如何使其返回字典中的所有单元格坐标?

【问题讨论】:

    标签: python


    【解决方案1】:

    你可以使用:

    def get_cells(size):
        result = []
        for x_axis in range(size):
            for y_axis in range(size):
                result.append((x_axis, y_axis))
        return result
    

    可以进一步简化(使用list comprehension)为:

    def get_cells(size):
        return [(x_axis, y_axis) for x_axis in range(size) for y_axis in range(size)]
    

    可以进一步简化(使用itertools.product)为:

    import itertools as IT
    def get_cells(size):
        return list(IT.product(range(size), repeat=size))
    

    请注意,根本没有必要为此定义函数,因为结果只是单行。您可以直接使用IT.product(range(size), repeat=size) 而不是定义get_cells


    In [1]: import itertools as IT
    
    In [2]: list(IT.product(range(2), repeat=2))
    Out[2]: [(0, 0), (0, 1), (1, 0), (1, 1)]
    

    【讨论】:

      【解决方案2】:

      我认为您更喜欢使用列表而不是字典来包含所有坐标作为元组。

      def get_cells(size):
          my_list = []
          for x_axis in range(0, size):
              x = x_axis
              for y_axis in range(0, size):
                  y = y_axis
                  my_list.append((x, y))
          return my_list
      

      【讨论】:

        【解决方案3】:

        你可以试试

          [(x,y) for x in xrange(0, size) for y in xrange(0, size)]
        

          ((x,y) for x in xrange(0, size) for y in xrange(0, size))
        

        假设您的意思是返回 iterable,而不是 dict

        【讨论】:

          【解决方案4】:

          你可以:

          创建一个新列表 (l = []),并将坐标附加到列表 (l+=[x, y])

          >>> def get_cells(size):
          ...     l = []
          ...     for x_axis in range(0, size):
          ...         x = x_axis
          ...         for y_axis in range(0, size):
          ...             y = y_axis
          ...             l.append([x, y])
          ...     return l
          ...
          >>> l = get_cells(2)
          >>> l
          [[0, 0], [0, 1], [1, 0], [1, 1]]
          

          或者使用yield 而不是return 来遍历您的单元格:

          >>> def get_cells(size):
          ...     for x_axis in range(0, size):
          ...         x = x_axis
          ...         for y_axis in range(0, size):
          ...             y = y_axis
          ...             yield (x, y)
          ...
          >>> for i in get_cells(2):
          ...   print i
          ...
          (0, 0)
          (0, 1)
          (1, 0)
          (1, 1)
          

          【讨论】:

            猜你喜欢
            • 2015-06-17
            • 2018-11-28
            • 2019-07-18
            • 1970-01-01
            • 1970-01-01
            • 2016-07-15
            • 1970-01-01
            • 1970-01-01
            • 2015-05-31
            相关资源
            最近更新 更多