简体   繁体   English

Python新手:如何从对象中提取一个元素?

[英]New to Python: how to extract one element from an object?

I'm new to python, so I don't really know exactly how to ask my question, so my terminology may be wrong. 我是python的新手,所以我真的不太清楚如何问我的问题,因此我的术语可能是错误的。 Anyway, I'm given some preliminary code that creates a matrix called subeta. 无论如何,我得到了一些初步的代码,这些代码创建了一个称为subeta的矩阵。 I need to work with subeta. 我需要使用subeta。 However, when I type 'subeta', I see that the object actually has much more information (see below) than the matrix I want to work with. 但是,当我键入'subeta'时,我看到该对象实际上比我要使用的矩阵具有更多的信息(见下文)。 How can I extract only the array (after the 'data':, not including the dtype=float32 part) from all the information that is stored in that object subeta? 我如何从存储在该对象子集中的所有信息中仅提取数组(在“数据”之后:,不包括dtype = float32部分)?

In [17]: subeta

Out[17]: 
{'data': array([[ 1.        ,  0.88093734,  0.87001401, ...,  0.65282464,
     0.59209341,  0.58587438],
   [ 0.88093734,  1.        ,  0.97301871, ...,  0.63097703,
     0.60524851,  0.60063201],
   [ 0.87001401,  0.97301871,  1.        , ...,  0.6584534 ,
     0.61063689,  0.5927977 ],
   ..., 
   [ 0.65282464,  0.63097703,  0.6584534 , ...,  1.        ,
     0.7761867 ,  0.72384161],
   [ 0.59209341,  0.60524851,  0.61063689, ...,  0.7761867 ,
     1.        ,  0.99335372],
   [ 0.58587438,  0.60063201,  0.5927977 , ...,  0.72384161,
     0.99335372,  1.        ]], dtype=float32),
'desc': 'Correlation matrix [channels,channels]',
'funcfile': '/Users/...[omitted]/newJJ/S3/func/func_res.nii.gz',
'mask': <nibabel.nifti1.Nifti1Image at 0x5344350>, 
'maskfile': '/Users/...[omitted]/S3/segment/gm2func.nii.gz /\\ sphere.nii.gz',
'maskthresh': 0.5,
'mode': 'fMRI', 
'type': 'VTT/eta2'}

subeta is a dictionary. subeta是一本字典。 To get a handle on the array, you can just do: 要获取数组的句柄,您可以执行以下操作:

array = subeta['data']

Now, array is a numpy ndarray which holds float32 objects (It's an array of 4-byte floats). 现在, array是一个numpy ndarray ,它保存float32对象(这是一个4字节浮点数的数组)。 ndarray objects carry around a lot of meta-data that can be very useful (including the dtype=float32 part which could be inspected from the .dtype attribute and other attributes like shape ). ndarray对象包含许多非常有用的元数据(包括dtype=float32部分,可以从.dtype属性和诸如shape其他属性中进行检查)。

Use: 采用:

subeta['data']

Or: 要么:

my_data = subeta['data']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM