使用索引将值插入2dpython列表

2024-04-25 00:14:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从几个python列表(即2D列表)中一次插入一个值到另一个2D列表中。(我知道numpy在这方面做得更好,但是我正在尝试比较列表和numpy的性能,所以请不要只是建议numpy。)我想在特定位置插入值,因此索引在左侧。在

重采样的“点”列表是一个240乘240的列表,而“点”列表是一个225乘225的列表。在

从示例的最后四行中,我得到的错误是“TypeError:'float'object is not subscriptable”。例如,我得到pix_prod_bl[0][0]是一个浮点数,但我不明白为什么不能将它插入重采样的“点”列表中的一组特定索引中。在

编辑1-添加了最小工作示例。在

编辑2-在添加工作示例时,我发现我不小心在将列表转换回numpy的地方有一行注释,并且不知何故我误解了Spyder控制台关于错误的来源。不管怎样,它现在起作用了,非常感谢你的快速反馈。我想我把这个放在这里,以防对其他人有帮助。在

edit3-pix_spot_值是一个数据数组,因此只需一个介于0和1之间的随机浮点数组就足够了。在

xc=57
yc=189     
rebin=15

# fraction pixel offset requiring interpolation
dx=xc*rebin-int(np.floor(xc*rebin)) # positive value between 0 and 1
dy=yc*rebin-int(np.floor(yc*rebin)) # positive value between 0 and 1

# weights for interpolation
w00=(1-dy)*(1-dx)
w10=dy*(1-dx)
w01=(1-dy)*dx
w11=dy*dx

# now the rest of the offset is an integer shift
dx=int(np.floor(xc*rebin))-int(np.floor(xc))*rebin # positive integer between 0 and 14
dy=int(np.floor(yc*rebin))-int(np.floor(yc))*rebin # positive integer between 0 and 14

def new_pix_spot(w00, w10, w01, w11, pix_spot_list, ny_spot, nx_spot, rebin, dy, dx):
    #first change numpy array to list
    pix_spot_list=pix_spot_values.tolist()

    #preallocate array of zeros
    resampled_pix_spot_list=[[0 for x in range (ny_spot + rebin)] for y in range(nx_spot+rebin)]

    #create 2D lists
    pix_prod_bl = [[x*w00 for x in y] for y in pix_spot_list]#bottom left
    pix_prod_br = [[x*w10 for x in y] for y in pix_spot_list]#bottom right
    pix_prod_tl = [[x*w01 for x in y] for y in pix_spot_list]#top left
    pix_prod_tr = [[x*w11 for x in y] for y in pix_spot_list]#top right

    for i in range (len(pix_spot_list)):
        for j in range (len(pix_spot_list)):

            k=dy + i
            m=dx + j
            n=dy + 1 + i
            p=dx + 1 + i

            resampled_pix_spot_list[k][m] += pix_prod_bl[i][j] #bottom left
            resampled_pix_spot_list[n][m] += pix_prod_br[i][j] #bottom right
            resampled_pix_spot_list[k][p] += pix_prod_tl[i][j] #top left
            resampled_pix_spot_list[n][p] += pix_prod_tr[i][j] #top right


    resampled_pix_spot_values = np.array(resampled_pix_spot_list)

    return resampled_pix_spot_values

Tags: innumpy列表fornpprodlistint
1条回答
网友
1楼 · 发布于 2024-04-25 00:14:06

插入和更换

要在Python中向列表中插入值,必须使用list对象(例如,resampled_pix_spot_list[0]),而不是其中的元素(resampled_pix_spot_list[0][0],正如您所尝试的那样)。在

在python2和python3中,可以使用your_list.insert(<index>, <element>)list insertion docs here)插入到列表中。在

因此,要在所选坐标的左边插入一个数字,代码是:

resampled_pix_spot_list[k].insert(m, pix_prod_bl[i][j])

如果您想替换该位置的像素,您可以写下:

^{pr2}$

(注意[k]vs[k][m])简而言之:插入,与列表对话;替换,与元素对话。在


重复插入的陷阱

提示:如果您计划在列表中的特定位置重复插入值,请尝试从列表末尾向后迭代。如果不这样做,则必须调整索引,因为每次.insert()调用都会将列表的一部分向右移动。在

为了理解我的意思,让我们假设我有一个列表[1, 2, 3],并希望通过插入来结束[1, 88, 2, 99, 3]。我们插入的顺序很重要。比较错误的顺序(向前迭代):

data = [1, 2, 3]
>>> data.insert(1, 88)
>>> print(data)
[1, 88, 2, 3]  # so far so good
>>> data.insert(2, 99)
>>> print(data)
[1, 88, 99, 2, 3]  # oops!  the first insert changed my indices, so index "2" was wrong!

以正确的顺序(向后迭代):

data = [1, 2, 3]
>>> data.insert(2, 99)
>>> print(data)
[1, 2, 99, 3]  # so far so good
>>> data.insert(1, 88)
>>> print(data)
[1, 88, 2, 99, 3]  # same insertions + different order = different results!

切片

一些值得思考的东西:Python2.7和3都允许您用一个非常干净的语法替换整个列表的“片段”,这也有助于避免“offbyone”错误(slice notation docs here)。例如:

>>> data = [1, 2, 3]
>>> data[1:2] = [88, data[1], 99]  # safer, shorter, faster, and clearer
>>> print(data)
[1, 88, 2, 99, 3]

使用切片可能更具声明性和清晰性。希望这有帮助!在

相关问题 更多 >