编辑无法在for循环中使用的数组

时间:2019-02-20 05:42:13

标签: python numpy numpy-ndarray

我正在处理一个包含几个嵌套数组作为元素的列表。每个数组代表一组要绘制的点,这些点将被绘制为动画散点图中的框架。每帧都包含前一帧作为子集,外加随机数的新点,以便在动画中,散点图的点数会增加,但是旧点会保留在原位。

点的颜色和大小作为信息存储在这些数组中,我正在尝试对其进行编辑,以使每个帧中最近渲染的5个帧中的点都突出显示。

但是,当我实际尝试实现突出显示时,对列表所做的更改似乎并不会在我的for循环中持续存在。谁能帮我弄清楚这里发生了什么事?

示例代码段

newFrames = frames.copy()
# n x 1 list of 2-d arrays representing frames

# the leading 5 frames are highlighted
for i in range(5):
    newFrames[i][:,2] = 1   #color set to black
    newFrames[i][:,3] = 100 #size set to 100

# all succeeding frames have the most recently added data highlighted
for i in range(5,len(newFrames)):
    newFrames[i][:,2] = 0.3
    newFrames[i][:,3] = 2

    # points from most recently added 5 frames are to be highlighted
    j = len(newFrames[i]) - len(newFrames[i-5])

    # highlighting
    newFrames[i][-j:,2] = 1
    newFrames[i][-j:,3] = 100

    # all other points are made small and dim
    newFrames[i][:-j,2] = 0.3 #color set to grey
    newFrames[i][:-j,3] = 2   #size set to 2

    print(newFrames[i][:,2:])

    '''
    Sample output
    [[   0.3    2. ]
    [   0.3    2. ]
    [   0.3    2. ]
    [   0.3    2. ]
    [   0.3    2. ]
    [   1.   100. ]
    [   1.   100. ]
    [   1.   100. ]
    [   1.   100. ] 
    [   1.   100. ]]
    '''
print(newFrames[i][:,2:])

'''
Sample output: what went wrong?
[[   0.3    2. ]
[   0.3    2. ]
[   0.3    2. ]
[   0.3    2. ]
[   0.3    2. ]
[   0.3    2. ]
[   0.3    2. ]
[   0.3    2. ]
[   0.3    2. ]
[   0.3    2. ]]
'''

1 个答案:

答案 0 :(得分:0)

因此,事实证明,在尝试更改阵列中的任何内容时,阵列的组织方式会导致大量问题。

最初,它是这样组织的:

frames = []
for i in range(10):
    frames.append(np.random.random((i, 4)))

由于某些原因,编辑存储在frames中的数组的元素本身并不会改变它们的框架。

我通过将frames构建为

解决了该问题。
frames = np.random.random((10,10,4))
for i in range(10):
    frames[i,i:]=-9999 #placeholder empty value