C-like structure in Python

Introduction

A technique that can be used when you are in agony and want to use a C-like structure in Python. For example, if you want to create a structure like ↓.

c_struct.h


typedef struct{
    float pos[4];
    unsigned char class;
    float score;
} HOGE;

HOGE fuga[10];

Expression method in Python

Use Structured Data types

c_like_struct.py


import numpy as np

HOGE = np.dtype([("pos",    np.float32, (4,)),
                 ("class",  np.uint8,   (1,)),
                 ("score",  np.float32, (1,)),
                 ],
                align=True
                )
fuga = np.zeros(10, dtype=HOGE)

With this, a C language-like structure can be expressed. Padding is done by setting align = True above.

How to use ① (output for the time being)

processing


print(HOGE)
print(fuga)

result


{'names':['pos','class','score'], 'formats':[('<f4', (4,)),('u1', (1,)),('<f4', (1,))], 'offsets':[0,16,20], 'itemsize':24, 'aligned':True}
[([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
 ([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
 ([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
 ([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
 ([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])]

How to use ② (dtype, shape)

processing


print("fuga.dtype: ",fuga.dtype)
print("fuga.shape: ",fuga.shape)
print("fuga.['pos'].shape: ",fuga['pos'].shape)
print("fuga.['pos'][0].shape: ",fuga['pos'][0].shape)
print("fuga['pos'][1]: ", fuga['pos'][1])

result


fuga.dtype:  {'names':['pos','class','score'], 'formats':[('<f4', (4,)),('u1', (1,)),('<f4', (1,))], 'offsets':[0,16,20], 'itemsize':24, 'aligned':True}
fuga.shape:  (10,)
fuga['pos'].shape:  (10, 4)
fuga['pos'][0].shape:  (4,)
fuga['pos'][1]:  [0. 0. 0. 0.]

How to use ③ (np.recarray)

processing


piyo = fuga.view(np.recarray)
print("piyo: ", piyo)
print("piyo.pos.shape: ", piyo.pos.shape)
print("piyo.pos[0].shape: ", piyo.pos[0].shape)
print("piyo.pos[1]: ",piyo.pos[1])

result


piyo:  [([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
        ([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
        ([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
        ([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])
        ([0., 0., 0., 0.], [0], [0.]) ([0., 0., 0., 0.], [0], [0.])]
piyo.pos.shape:  (10, 4)
piyo.pos[0].shape:  (4,)
piyo.pos[1]:  [0. 0. 0. 0.]

Usage check ④ (assignment process)

processing


fuga['pos'][1] = [-1.0 , 2.3, 0.42, 0.0]
print("fuga['pos'][:3]: ",fuga['pos'][:3])

result


fuga['pos'][:3]:  [[ 0.    0.    0.    0.  ]
                   [-1.    2.3   0.42  0.  ]
                   [ 0.    0.    0.    0.  ]]

Recommended Posts

C-like structure in Python
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
Python internal structure
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Output tree structure of files in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Solve ABC168D in Python
Daily AtCoder # 7 in Python
LU decomposition in Python
One liner in Python