python - Copying a list of lists into part of another one in the most pythonic way -
i have large list of lists, containing zeros. have another, smaller list of lists want copy positions in bigger list of lists. want replace original values. way i've found far this:
start = (startrow, startcol) in range( start[0], start[0] + len(shortlist) ): j in range( start[1], start[1] + len(shortlist[0]) ): longlist[i][j] = shortlist[i-start[0]][j-start[1]]
however, doesn't feel pythonic - far know, list comprehensions in general prefered loops, , though didn't find in few hours of searching, there might function doing more elegantly. there better solution mine?
edit: i'm interested in numpy solutions well, or perhaps more in plain python ones.
you can replace inner loop slice-replacement:
start = (startrow, startcol) i, elements in enumerate(shortlist, start[0]): longlist[i][start[1]:start[1] + len(elements)] = elements
depending on want do, using numpy
-arrays alternative:
large[startrow:startrow + small.shape[0], startcol:startcol + small.shape[1]] = small
Comments
Post a Comment