This is the 18th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Numpy’s structured array is similar to the Python concept of class, consisting of a set of field names with specific meanings and specific data types:

In [1]: import numpy as np In [2]: data = np.array([('Mike', 18, 'SZ'), ('Jerry', 19, 'BJ') ...: ], dtype=[('name', 'U5'), ('age', 'i4'), ('city', 'U2')] ... : ) In [3]: data Out[3]: array([('Mike', 18, 'SZ'), ('Jerry', 19, 'BJ')], dtype=[('name', '<U5'), ('age', '<i4'), ('city', '<U2')])Copy the code

Data is a one-dimensional array with two elements of length 2, each of which has the following three fields:

Name: a string of 5 characters or less Age: an integer of 4 bytes City: a string of 2 characters or lessCopy the code

For structured arrays, you can either get the data using a regular integer index, or you can get the data directly by the field name (the effect is similar to getting a column of data in Excel):

In [3]: data
Out[3]:
array([('Mike', 18, 'SZ'), ('Jerry', 19, 'BJ')],
      dtype=[('name', '<U5'), ('age', '<i4'), ('city', '<U2')])

In [4]: data[0]
Out[4]: ('Mike', 18, 'SZ')

In [5]: data['city']
Out[5]: array(['SZ', 'BJ'], dtype='<U2')

In [6]: data['name']
Out[6]: array(['Mike', 'Jerry'], dtype='<U5')

In [7]: data['age']
Out[7]: array([18, 19], dtype=int32)

In [8]: data['city'][0]
Out[8]: 'SZ'
Copy the code

Query the name of a structured array:

In [10]: data.dtype
Out[10]: dtype([('name', '<U5'), ('age', '<i4'), ('city', '<U2')])

In [12]: data.dtype.names
Out[12]: ('name', 'age', 'city')

In [13]: data.dtype.fields
Out[13]:
mappingproxy({'name': (dtype('<U5'), 0),
              'age': (dtype('int32'), 20),
              'city': (dtype('<U2'), 24)})
Copy the code

Dtype.names returns the list of field names for this structured array; Dtype. fields returns a dictionary-like object with a tuple of field name key, field data type, and offset as key.

To obtain structured data by field name, we obtain a view of the shared memory. Modifying the data of any array will affect all arrays that share this memory:

In [14]: names = data['name']

In [15]: names
Out[15]: array(['Mike', 'Jerry'], dtype='<U5')

In [16]: data
Out[16]:
array([('Mike', 18, 'SZ'), ('Jerry', 19, 'BJ')],
      dtype=[('name', '<U5'), ('age', '<i4'), ('city', '<U2')])

In [17]: names[:] = 'Undefined'

In [18]: names
Out[18]: array(['Undef', 'Undef'], dtype='<U5')

In [19]: data
Out[19]:
array([('Undef', 18, 'SZ'), ('Undef', 19, 'BJ')],
      dtype=[('name', '<U5'), ('age', '<i4'), ('city', '<U2')])
Copy the code