Skip to content

Instantly share code, notes, and snippets.

@vitalizzare
Created April 27, 2021 16:20
Show Gist options
  • Select an option

  • Save vitalizzare/ca8379ac14d1a603c2cd41a5e273200f to your computer and use it in GitHub Desktop.

Select an option

Save vitalizzare/ca8379ac14d1a603c2cd41a5e273200f to your computer and use it in GitHub Desktop.
Example of using np.dtype to structure data
var_space = np.dtype([
('name', 'S5'), # 5 bytes for the name
('age', 'i1'), # 1 byte for the age
('weight', 'f4') # 4 bytes for the weight value
]) # 10 bytes in total to describe 1 person
def init_persons(n:int) -> np.ndarray:
'''Get initial values for n persons.
Return numpy.ndarray of dtype var_space'''
persons = np.zeros(n, dtype=var_space)
for i in range(n):
persons[i]['name'] = input('Name [5 letters]: ')
persons[i]['age'] = int(input('Age: '))
persons[i]['weight'] = float(input('Weight, kg: '))
return persons
n = 3
persons = init_persons(n)
for i, record in enumerate(persons):
print('{}. {}, size={}'.format(i, record, record.itemsize))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment