Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:在另一个替换内容中插入2D列表_Python_List_Matrix - Fatal编程技术网

Python:在另一个替换内容中插入2D列表

Python:在另一个替换内容中插入2D列表,python,list,matrix,Python,List,Matrix,我编写了一个小的(破坏性的)函数,它接受两个2D列表(或者我称之为“网格”)和一组坐标。它将第一个网格插入到第二个网格中,假设第一个网格与第一个网格一样大(未执行检查)。坐标表示第一个栅格的左上角 def insert_grid(subj, obj, cords=(0, 0)): u, v = cords h = len(subj) w = len(subj[0]) for y in range(0, h): for x in range(0, w):

我编写了一个小的(破坏性的)函数,它接受两个2D列表(或者我称之为“网格”)和一组坐标。它将第一个网格插入到第二个网格中,假设第一个网格与第一个网格一样大(未执行检查)。坐标表示第一个栅格的左上角

def insert_grid(subj, obj, cords=(0, 0)):
   u, v = cords
   h = len(subj)
   w = len(subj[0])
   for y in range(0, h):
      for x in range(0, w):
         obj[u + y][v + x] = subj[y][x]
我想知道是否有一种更干净、更通灵的方法来达到同样的效果。标准的lib方法将一如既往地优先于其他方法


多谢各位。Alisa.

您可以稍微简化:

def insert_grid(subj, obj, cords=(0, 0)):
    u, v = cords
    w = len(subj[0])        
    for index, row in enumerate(subj, u):
        obj[index][v:v+w] = row

这将一次性替换行中的所有列

是的,我真的应该让使用切片成为我的第二天性。C心态变化不快。