【问题标题】:Slicing python lists切片python列表
【发布时间】:2010-07-12 15:20:36
【问题描述】:

如果我有一个表示一个矩形二维矩阵的说“n”个元素的列表(每个元素是一个单字节),我如何从列表的第一个元素开始将其拆分为 w * h 的矩形,只使用python标准函数

例如

l =  
 [ 1,2,3,4,5,6,7,8,9,10,  
   11,12,13,14,15....20.  
   21,22,23,24,25....30  
   .....    
   .................200]   

这些都在一维列表中

如果我们选择 2*3 (w*h) 的矩形 第一个将包含 1,2,11,12,21,22 第二个将包含 3,4,13,14,23,24 等等直到结束

谢谢

【问题讨论】:

  • 您能否提供一个输入示例以及您希望输出的内容?
  • “标准函数”是什么意思?听起来您正在寻找一种特定风格的代码。为什么不满足于完成工作的代码呢?
  • 输入list是1D吗?如果是这样,按行优先还是列优先?
  • 嗨,我的意思是你用 python 2.6 得到的标准东西(不是 numpy)
  • “第二个将包含 4,5,13,​​14,23,24”——这不是错字吗,应该是“3,4”而不是“4,5”?跨度>

标签: python list slice


【解决方案1】:

请注意,您的问题指定输入列表是一维的,但没有说明每个逻辑行有多少项;你似乎神奇地暗示它应该是每行 10 个项目。

因此,给定一维列表、每行逻辑项的计数、请求的图块的宽度和高度,您可以这样做:

def gettiles(list1d, row_items, width, height):
    o_row= 0
    row_count, remainder= divmod(len(list1d), row_items)
    if remainder != 0:
        raise RuntimeError("item count not divisible by %d" % row_items)
    if row_count % height != 0:
        raise RuntimeError("row count not divisible by height %d" % height)
    if row_items % width != 0:
        raise RuntimeError("row width not divisible by %d" % width)
    for o_row in xrange(0, row_count, height):
        for o_col in xrange(0, row_items, width):
            result= []
            top_left_index= o_row*row_items + o_col
            for off_row in xrange(height):
                for off_col in xrange(width):
                    result.append(list1d[top_left_index + off_row*row_items + off_col])
            yield result

>>> import pprint
>>> pprint.pprint(list(gettiles(range(100), 10, 2, 5)))
[[0, 1, 10, 11, 20, 21, 30, 31, 40, 41],
 [2, 3, 12, 13, 22, 23, 32, 33, 42, 43],
 [4, 5, 14, 15, 24, 25, 34, 35, 44, 45],
 [6, 7, 16, 17, 26, 27, 36, 37, 46, 47],
 [8, 9, 18, 19, 28, 29, 38, 39, 48, 49],
 [50, 51, 60, 61, 70, 71, 80, 81, 90, 91],
 [52, 53, 62, 63, 72, 73, 82, 83, 92, 93],
 [54, 55, 64, 65, 74, 75, 84, 85, 94, 95],
 [56, 57, 66, 67, 76, 77, 86, 87, 96, 97],
 [58, 59, 68, 69, 78, 79, 88, 89, 98, 99]]

【讨论】:

    【解决方案2】:
    width = 6
    height = 4
    xs = range(1,25)
    w = 3
    h = 2
    
    def subrect(x,y):
        pos = y*h*width+x*w
        return [xs[(pos+row*width):(pos+row*width+w)] for row in range(h)]
    
    print [subrect(x,y) for y in range(height / h) for x in range(width / w)]
    

    按如下方式拆分矩阵:

     1  2  3     4  5  6
     7  8  9    10 11 12
    
    13 14 15    16 17 18
    19 20 21    22 23 24
    

    编辑:或者你给出的例子......

    width = 10
    height = 20
    xs = range(1,201)
    w = 2
    h = 3
    

    【讨论】:

      【解决方案3】:

      或者这个,很简单。

      def genMatrix(rows, cols, mylist):
         for x in xrange(rows):
            yield mylist[x*cols:x*cols+cols]
      

      结果

      >>> L = [1,1,1,1,2,2,2,2]
      >>> list(genMatrix(2, 4, L))
      [[1, 1, 1, 1], [2, 2, 2, 2]]
      >>> L = [1,1,1,1,2,2,2,2,3,3,3,3]
      >>> list(genMatrix(3, 4, L))
      [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]
      

      【讨论】:

        【解决方案4】:

        这是一个建议(可能效率很低,但似乎有效):

        def rect_slice(seq, cols, width, height):
            rows = len(seq) // cols
            for i in xrange(0, rows - rows % height, height):
                for j in xrange(0, cols - cols % width, width):
                    yield [seq[k * cols + l] for k in xrange(i, i + height) for l in xrange(j, j + width)]
        
        print list(rect_slice(range(1, 201), 10, 2, 3))
        

        【讨论】:

          【解决方案5】:

          如果我理解正确,您有[1,1,1,1,2,2,2,2] 并想要[[1,1,1,1], [2,2,2,2]]?如果是这样,那很简单:

          L = [1,1,1,1,2,2,2,2]
          w = 4
          matrix = [L[:w], L[w:]]
          

          至少 2d。

          或者你可以写一个更通用的解决方案:

          def genMatrix(rows, cols, mylist):
             matrix = []
             for x in xrange(rows):
                row = []
                for y in xrange(cols):
                   row.append(mylist[x*cols])
                matrix.append(row)
             return matrix
          
          print genMatrix(2, 4, L) # => [[1,1,1,1], [2,2,2,2]]
          L = [1,1,1,1,2,2,2,2,3,3,3,3]
          print getnMatrix(3, 4, L) # => [[1,1,1,1], [2,2,2,2], [3,3,3,3]]
          

          【讨论】:

            猜你喜欢
            • 2017-06-24
            • 2015-02-19
            • 2014-05-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-16
            • 2014-11-02
            • 1970-01-01
            相关资源
            最近更新 更多