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

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

Comments

Your Answer

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.