2

We both know that: "Numpy array is multidimensional array of objects of all the same type"

However, I could create a Numpy array that contains different data types as example below. Can anyone give an explain, how it could be.

import numpy as np
a = np.array([('a',1),('b',2)],dtype=[('alpha','U11'),('num','i8')])
print(a[0][1]+1)
print(len(a[0][0]))

Output:

2
1
asked Aug 1, 2021 at 8:32
1
  • print a, and its shape. And index by fild name Commented Aug 1, 2021 at 9:14

2 Answers 2

4

Those are numpy records:

Numpy provides two data structures, the homogeneous arrays and the structured (aka record) arrays. The latter one, what you just stumbled across, is a structure that not only allows you to have different data types (float, int, str, etc.) but also provides handy methods to access them, through labels for instance.

answered Aug 1, 2021 at 8:37
Sign up to request clarification or add additional context in comments.

Comments

1

In Numpy: it's called Structured arrays

Please read more here: https://numpy.org/doc/stable/user/basics.rec.html

P/S: thanks Brandt

answered Aug 1, 2021 at 8:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.