在python中解压缩数组 [英] Unpacking an array in python

查看:66
本文介绍了在python中解压缩数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形状为(1000L, 3L)的变量数据,我执行以下操作以获取坐标:

I have a variable data that is of (1000L, 3L) shape and I do the following to get the coordinates:

x = data[:,0]
y = data[:,1]
z = data[:,2]

有没有办法拆开它们的包装?我试过了,但是不起作用:

Is there a way to unpack them? I tried but it doesn't work:

[x,y,z] = data1[:,0:3]

推荐答案

您可以简单地

You could simply transpose it before unpacking:

x, y, z = data.T

解包解包"数组的第一个维度,并通过转置数组将size-3维度作为第一个维度.这就是为什么它不适用于[x, y, z] = data1[:, 0:3]的原因,因为它试图将1000个值分解为3个变量.

Unpacking "unpacks" the first dimensions of an array and by transposing the your array the size-3 dimension will be the first dimension. That's why it didn't work with [x, y, z] = data1[:, 0:3] because that tried to unpack 1000 values into 3 variables.

这篇关于在python中解压缩数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆