简体   繁体   中英

Numpy structured array document confusion

I am a total newbie to numpy structured arrays.

If I use the code from the docs (see here: Numpy Structured Array )

import numpy as np 
x=np.array([(1,2.,'Hello'), (2,3.,"World")], dtype=[('foo', 'i4'),('bar', 'f4'), ('baz', 'S10')])
x
y = x['foo']
y

x is correct: array([(1, 2.0, 'Hello'), (2, 3.0, 'World')], dtype=[('foo', '<i4'), ('bar', '<f4'), ('baz', 'S10')])

However y gives me array([1, 2]) and the docs say it should be array([ 2., 3.], dtype=float32)

I have a hard time believing the docs are wrong but this code is so short and and I copied/pasted it into python.

Am I doing something wrong?

x['foo'] gives you an array of all first elements of each structure, so [1,2] is correct. The documentation mistakenly switched foo and bar , so read the docs as

y = x['bar']

and the rest of the example is correct.

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