删除 Python 中的方括号

Oma*_*mar 0 python list flatten python-3.x

我有这个输出:

[[[-0.015,  -0.1533,  1.    ]]

 [[-0.0069,  0.1421,  1.    ]]

...

 [[ 0.1318, -0.4406,  1.    ]]

 [[ 0.2059, -0.3854,  1.    ]]]
Run Code Online (Sandbox Code Playgroud)

但我想删除剩余的方括号,如下所示:

[[-0.015  -0.1533  1.    ]

 [-0.0069  0.1421  1.    ]

 ...

 [ 0.1318 -0.4406  1.    ]

 [ 0.2059 -0.3854  1.    ]]
Run Code Online (Sandbox Code Playgroud)

我的代码是这样的:

XY = []
for i in range(4000):
     Xy_1 = [round(random.uniform(-0.5, 0.5), 4), round(random.uniform(-0.5, 0.5), 4), 1]
     Xy_0 = [round(random.uniform(-0.5, 0.5), 4), round(random.uniform(-0.5, 0.5), 4), 0]
     Xy.append(random.choices(population=(Xy_0, Xy_1), weights=(0.15, 0.85)))

Xy = np.asarray(Xy)
Run Code Online (Sandbox Code Playgroud)

Dis*_*ani 5

您可以使用numpy.squeeze从阵列中删除 1 个暗淡

>>> np.squeeze(Xy)
array([[ 0.3609,  0.2378,  0.    ],
       [-0.2432, -0.2043,  1.    ],
       [ 0.3081, -0.2457,  1.    ],
       ...,
       [ 0.311 ,  0.03  ,  1.    ],
       [-0.0572, -0.317 ,  1.    ],
       [ 0.3026,  0.1829,  1.    ]])
Run Code Online (Sandbox Code Playgroud)

或重塑使用numpy.reshape

>>> np.squeeze(Xy)
array([[ 0.3609,  0.2378,  0.    ],
       [-0.2432, -0.2043,  1.    ],
       [ 0.3081, -0.2457,  1.    ],
       ...,
       [ 0.311 ,  0.03  ,  1.    ],
       [-0.0572, -0.317 ,  1.    ],
       [ 0.3026,  0.1829,  1.    ]])
Run Code Online (Sandbox Code Playgroud)