You want to put together information about a person, such as name, age, and height. In that case, use NumPy's structured array. You can efficiently store different types of complex data. Since it's a big deal, I tried to compose it with Nogizaka members. (As of 02/24/2020)
list.py
# -*- coding: utf-8
import numpy as np
#Store information of 4 people in each array
name = ['mai', 'asuka', 'erika', 'yoda']
age = [27, 21, 23, 19]
height = [162, 158, 160, 152]
#Creating a structured array(Determine the data type of 3 types of arrays)
data = np.zeros(4, dtype={'names' : ('name', 'age', 'height'), 'formats': ('U10', 'i4', 'f8')}) #U10 -> Unicode(Up to 10 characters), i4 -> int32, f8 -> float64
#Data type confirmation
print(data.dtype)
#[('name', '<U10'), ('age', '<i4'), ('height', '<f8')]
#Store data in a structured array
data['name'] = name
data['age'] = age
data['height'] = height
#Print all information of 4 people
print(data)
#[(u'mai', 27, 162.0) (u'asuka', 21, 158.0) (u'erika', 23, 160.0) (u'yoda', 19, 152.0)]
#Print the names of four people
print(data['name'])
#[u'mai' u'asuka' u'erika' u'yoda']
#Print all asuka information
print(data[1])
#(u'asuka', 21, 158.0)
#Print mai height
print(data[0]['height'])
#162.0
O'Reilly Japan: Python Data Science Handbook
Recommended Posts