简体   繁体   中英

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. Anyway, I'm given some preliminary code that creates a matrix called subeta. I need to work with 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. 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?

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. 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). 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 ).

Use:

subeta['data']

Or:

my_data = subeta['data']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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