Array 編集

Pythonの array モジュールは、効率的な数値計算のために配列を提供するものです。通常のリストよりもメモリ効率が高く、要素の型を指定できるのが特徴です[1]。 JavaScript の TypedArray や、C言語等の静的型付けの言語の配列に相当します。

通常、NumPyライブラリがより高度な数値計算のニーズに対応するために使用されます。

array モジュールを使用するには、まずモジュールをインポートします。以下は簡単な使用例です:

from array import array

# 'd' は倍精度浮動小数点型を示します
float_array = array('d', [1.0, 2.0, 3.0])

# 配列の要素へのアクセス
print(float_array[0])  # 出力: 1.0
print(float_array[1])  # 出力: 2.0
print(float_array[2])  # 出力: 3.0

ここで、'd'double(倍精度浮動小数点数)型を示しています。他にもいくつかの型が利用可能で、例えば 'i' は整数型を表します。

from array import array

# 'i' は符号付き整数型を示します
int_array = array('i', [1, 2, 3])

# 配列の要素へのアクセス
print(int_array[0])  # 出力: 1
print(int_array[1])  # 出力: 2
print(int_array[2])  # 出力: 3

array モジュールは通常、数値計算が必要な場合や大量のデータを扱う際に使用されます。ただし、リストよりも効率的である一方で柔軟性は少なく、同じ型の要素しか格納できません。

操作とイテレーション 編集

Pythonの array モジュールで生成されたオブジェクトは、通常のリストと似たように操作できますが、要素の型が制約されているため、一部の操作が異なります。以下に、array オブジェクトの生成、操作、イテレーションの例を示します。

from array import array

# 'd' は倍精度浮動小数点型を示します
float_array = array('d', [1.0, 2.0, 3.0])
print(f"{float_array=}") # => float_array=array('d', [1.0, 2.0, 3.0])
print(f"{type(float_array)=}") # => type(float_array)=<class 'array.array'>

# 要素へのアクセス
print(float_array[0])  # 出力: 1.0

# スライス
subset = float_array[1:3]  # 2番目から3番目までの要素を取得
print(f"{subset=}") # => subset=array('d', [2.0, 3.0])
print(f"{type(subset)=}") # => type(subset)=<class 'array.array'>

# 要素の変更
float_array[1] = 4.0

# 要素の追加(末尾に追加)
float_array.append(5.0)

# イテレーション
for element in float_array:
    print(element)

# 要素へのアクセス
print(float_array[0])  # 出力: 1.0

# スライス
subset = float_array[1:3]  # 2番目から3番目までの要素を取得

# 要素の変更
float_array[1] = 4.0

# 要素の追加(末尾に追加)
float_array.append(5.0)

# イテレーション
for element in float_array:
    print(element)

array オブジェクトは通常のリストと同じようにイテレーションできます。要素の型が指定されているため、それに従って適切な操作を行うことが重要です。例えば、倍精度浮動小数点型の d を指定した場合、array オブジェクトの要素は浮動小数点数として格納されます。

コンストラクター 編集

arrayオブジェクトは、arrayモジュールのarray() です。

関数定義
def array(typecode, [inittialize-iteratable]):
arrayのtypecodeと特性
typecode C言語の型 要素のバイト数
'b' signed char 1
'B' unsigned char 1
'u' wchar_t 4
'h' signed short 2
'H' unsigned short 2
'i' signed int 4
'I' unsigned int 4
'l' signed long 8
'L' unsigned long 8
'q' signed long long 8
'Q' unsigned long long 8
'f' float 4
'd' double 8
from array import array, typecodes

print("""\
{| class="wikitable"
|+ arrayのtypecodeと特性
!typecode!!C言語の型!!要素のバイト数\
""")
for typecode, ctype in zip(typecodes,
    "signed char,unsigned char,wchar_t,signed short,unsigned short,signed int,unsigned int,signed long,unsigned long,signed long long,unsigned long long,float,double".split(",")):
    ary = array(typecode, "ABCDEF" if typecode == 'u' else range(10))
    print(f'''\
|-
! {repr(typecode)}
| {ctype} 
|style='text-align:right'| {ary.itemsize}\
''')

print("|}")
コンストラクター
from array import array, typecodes

for typecode in typecodes:
    ary = array(typecode, "ABCDEF" if typecode == 'u' else range(10))
    print(f'''\
{ary.typecode=} type={type(ary[0]).__name__} {ary.itemsize=}
{ary=}
{len(ary)=}
{ary[3]=}
{ary[1:-2]=}
{ary[1:-2:2]=}
{list(ary)=}
{ary.tolist()=}
''')
実行結果
ary.typecode='b' type=int ary.itemsize=1
ary=array('b', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
len(ary)=10
ary[3]=3
ary[1:-2]=array('b', [1, 2, 3, 4, 5, 6, 7])
ary[1:-2:2]=array('b', [1, 3, 5, 7])
list(ary)=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.tolist()=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ary.typecode='B' type=int ary.itemsize=1
ary=array('B', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
len(ary)=10
ary[3]=3
ary[1:-2]=array('B', [1, 2, 3, 4, 5, 6, 7])
ary[1:-2:2]=array('B', [1, 3, 5, 7])
list(ary)=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.tolist()=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ary.typecode='u' type=str ary.itemsize=4
ary=array('u', 'ABCDEF')
len(ary)=6
ary[3]='D'
ary[1:-2]=array('u', 'BCD')
ary[1:-2:2]=array('u', 'BD')
list(ary)=['A', 'B', 'C', 'D', 'E', 'F']
ary.tolist()=['A', 'B', 'C', 'D', 'E', 'F']

ary.typecode='h' type=int ary.itemsize=2
ary=array('h', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
len(ary)=10
ary[3]=3
ary[1:-2]=array('h', [1, 2, 3, 4, 5, 6, 7])
ary[1:-2:2]=array('h', [1, 3, 5, 7])
list(ary)=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.tolist()=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ary.typecode='H' type=int ary.itemsize=2
ary=array('H', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
len(ary)=10
ary[3]=3
ary[1:-2]=array('H', [1, 2, 3, 4, 5, 6, 7])
ary[1:-2:2]=array('H', [1, 3, 5, 7])
list(ary)=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.tolist()=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ary.typecode='i' type=int ary.itemsize=4
ary=array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
len(ary)=10
ary[3]=3
ary[1:-2]=array('i', [1, 2, 3, 4, 5, 6, 7])
ary[1:-2:2]=array('i', [1, 3, 5, 7])
list(ary)=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.tolist()=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ary.typecode='I' type=int ary.itemsize=4
ary=array('I', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
len(ary)=10
ary[3]=3
ary[1:-2]=array('I', [1, 2, 3, 4, 5, 6, 7])
ary[1:-2:2]=array('I', [1, 3, 5, 7])
list(ary)=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.tolist()=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ary.typecode='l' type=int ary.itemsize=8
ary=array('l', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
len(ary)=10
ary[3]=3
ary[1:-2]=array('l', [1, 2, 3, 4, 5, 6, 7])
ary[1:-2:2]=array('l', [1, 3, 5, 7])
list(ary)=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.tolist()=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ary.typecode='L' type=int ary.itemsize=8
ary=array('L', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
len(ary)=10
ary[3]=3
ary[1:-2]=array('L', [1, 2, 3, 4, 5, 6, 7])
ary[1:-2:2]=array('L', [1, 3, 5, 7])
list(ary)=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.tolist()=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ary.typecode='q' type=int ary.itemsize=8
ary=array('q', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
len(ary)=10
ary[3]=3
ary[1:-2]=array('q', [1, 2, 3, 4, 5, 6, 7])
ary[1:-2:2]=array('q', [1, 3, 5, 7])
list(ary)=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.tolist()=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ary.typecode='Q' type=int ary.itemsize=8
ary=array('Q', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
len(ary)=10
ary[3]=3
ary[1:-2]=array('Q', [1, 2, 3, 4, 5, 6, 7])
ary[1:-2:2]=array('Q', [1, 3, 5, 7])
list(ary)=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.tolist()=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ary.typecode='f' type=float ary.itemsize=4
ary=array('f', [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
len(ary)=10
ary[3]=3.0
ary[1:-2]=array('f', [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
ary[1:-2:2]=array('f', [1.0, 3.0, 5.0, 7.0])
list(ary)=[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
ary.tolist()=[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

ary.typecode='d' type=float ary.itemsize=8
ary=array('d', [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
len(ary)=10
ary[3]=3.0
ary[1:-2]=array('d', [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
ary[1:-2:2]=array('d', [1.0, 3.0, 5.0, 7.0])
list(ary)=[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
ary.tolist()=[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

arrayと演算子 編集

arrayと演算子
from array import array

for code in ('b', 'i', 'd'):
    ary = array(code, [i for i in range(10)])
    print(f'''\
{type(ary)=}
{ary=}
{ary+ary=}
{ary*3=}
{3*ary=}
''')
実行結果
li=[10, 'ABC', 30]
li[1]='ABC'
li[-1]=30
li + [2,3,4]=[10, 'ABC', 30, 2, 3, 4]
li * 2=[10, 'ABC', 30, 10, 'ABC', 30]
2 * li=[10, 'ABC', 30, 10, 'ABC', 30]
li == [10 ,"ABC" ,30]=True
li != [10 ,"ABC" ,30]=False
li < [10 ,"ABC" ,30]=False
li > [10 ,"ABC" ,30]=False
arrayの要素は、インデックス演算子で参照できます。
array内の要素の順位はタプルやレンジと同じく、0番から始まります。
負のインデクスiは、len(self)+iを表します。
array同士の足し算は、連結した新しいarrayを返します。
arraytと整数nとの乗算は、tをn回繰り返したarrayを返します。
整数nとarraytとの乗算は、tをn回繰り返したarrayを返します。
array同士は、比較や大小判別ができます。

arrayはミュータブル 編集

arrayの各要素を、代入・置き換えすることもできます。

arrayはミュータブル
from array import array

for code in ('b', 'i', 'd'):
    ary = array(code, [i for i in range(10)])
    ary[8] = 100
    print(f'''\
{type(ary)=}
{ary=}
''')
実行結果
type(ary)=<class 'array.array'>
ary=array('b', [0, 1, 2, 3, 4, 5, 6, 7, 100, 9])

type(ary)=<class 'array.array'>
ary=array('i', [0, 1, 2, 3, 4, 5, 6, 7, 100, 9])

type(ary)=<class 'array.array'>
ary=array('d', [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 100.0, 9.0])

arrayのフィールド一覧 編集

arrayのフィールド一覧
from array import array

for code in ('b', 'i', 'd'):
    ary = array(code, [i for i in range(10)])
    print(f'''\
{type(ary)=}
{ary=}
''')
    for i in dir(ary):
        print(i, eval(f"type({ary}.{i})"))
実行結果
type(ary)=<class 'array.array'>
ary=array('b', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

__add__ <class 'method-wrapper'>
__class__ <class 'type'>
__contains__ <class 'method-wrapper'>
__copy__ <class 'builtin_function_or_method'>
__deepcopy__ <class 'builtin_function_or_method'>
__delattr__ <class 'method-wrapper'>
__delitem__ <class 'method-wrapper'>
__dir__ <class 'builtin_function_or_method'>
__doc__ <class 'str'>
__eq__ <class 'method-wrapper'>
__format__ <class 'builtin_function_or_method'>
__ge__ <class 'method-wrapper'>
__getattribute__ <class 'method-wrapper'>
__getitem__ <class 'method-wrapper'>
__gt__ <class 'method-wrapper'>
__hash__ <class 'NoneType'>
__iadd__ <class 'method-wrapper'>
__imul__ <class 'method-wrapper'>
__init__ <class 'method-wrapper'>
__init_subclass__ <class 'builtin_function_or_method'>
__iter__ <class 'method-wrapper'>
__le__ <class 'method-wrapper'>
__len__ <class 'method-wrapper'>
__lt__ <class 'method-wrapper'>
__mul__ <class 'method-wrapper'>
__ne__ <class 'method-wrapper'>
__new__ <class 'builtin_function_or_method'>
__reduce__ <class 'builtin_function_or_method'>
__reduce_ex__ <class 'builtin_function_or_method'>
__repr__ <class 'method-wrapper'>
__rmul__ <class 'method-wrapper'>
__setattr__ <class 'method-wrapper'>
__setitem__ <class 'method-wrapper'>
__sizeof__ <class 'builtin_function_or_method'>
__str__ <class 'method-wrapper'>
__subclasshook__ <class 'builtin_function_or_method'>
append <class 'builtin_function_or_method'>
buffer_info <class 'builtin_function_or_method'>
byteswap <class 'builtin_function_or_method'>
count <class 'builtin_function_or_method'>
extend <class 'builtin_function_or_method'>
frombytes <class 'builtin_function_or_method'>
fromfile <class 'builtin_function_or_method'>
fromlist <class 'builtin_function_or_method'>
fromstring <class 'builtin_function_or_method'>
fromunicode <class 'builtin_function_or_method'>
index <class 'builtin_function_or_method'>
insert <class 'builtin_function_or_method'>
itemsize <class 'int'>
pop <class 'builtin_function_or_method'>
remove <class 'builtin_function_or_method'>
reverse <class 'builtin_function_or_method'>
tobytes <class 'builtin_function_or_method'>
tofile <class 'builtin_function_or_method'>
tolist <class 'builtin_function_or_method'>
tostring <class 'builtin_function_or_method'>
tounicode <class 'builtin_function_or_method'>
typecode <class 'str'>
type(ary)=<class 'array.array'>
ary=array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

__add__ <class 'method-wrapper'>
__class__ <class 'type'>
__contains__ <class 'method-wrapper'>
__copy__ <class 'builtin_function_or_method'>
__deepcopy__ <class 'builtin_function_or_method'>
__delattr__ <class 'method-wrapper'>
__delitem__ <class 'method-wrapper'>
__dir__ <class 'builtin_function_or_method'>
__doc__ <class 'str'>
__eq__ <class 'method-wrapper'>
__format__ <class 'builtin_function_or_method'>
__ge__ <class 'method-wrapper'>
__getattribute__ <class 'method-wrapper'>
__getitem__ <class 'method-wrapper'>
__gt__ <class 'method-wrapper'>
__hash__ <class 'NoneType'>
__iadd__ <class 'method-wrapper'>
__imul__ <class 'method-wrapper'>
__init__ <class 'method-wrapper'>
__init_subclass__ <class 'builtin_function_or_method'>
__iter__ <class 'method-wrapper'>
__le__ <class 'method-wrapper'>
__len__ <class 'method-wrapper'>
__lt__ <class 'method-wrapper'>
__mul__ <class 'method-wrapper'>
__ne__ <class 'method-wrapper'>
__new__ <class 'builtin_function_or_method'>
__reduce__ <class 'builtin_function_or_method'>
__reduce_ex__ <class 'builtin_function_or_method'>
__repr__ <class 'method-wrapper'>
__rmul__ <class 'method-wrapper'>
__setattr__ <class 'method-wrapper'>
__setitem__ <class 'method-wrapper'>
__sizeof__ <class 'builtin_function_or_method'>
__str__ <class 'method-wrapper'>
__subclasshook__ <class 'builtin_function_or_method'>
append <class 'builtin_function_or_method'>
buffer_info <class 'builtin_function_or_method'>
byteswap <class 'builtin_function_or_method'>
count <class 'builtin_function_or_method'>
extend <class 'builtin_function_or_method'>
frombytes <class 'builtin_function_or_method'>
fromfile <class 'builtin_function_or_method'>
fromlist <class 'builtin_function_or_method'>
fromstring <class 'builtin_function_or_method'>
fromunicode <class 'builtin_function_or_method'>
index <class 'builtin_function_or_method'>
insert <class 'builtin_function_or_method'>
itemsize <class 'int'>
pop <class 'builtin_function_or_method'>
remove <class 'builtin_function_or_method'>
reverse <class 'builtin_function_or_method'>
tobytes <class 'builtin_function_or_method'>
tofile <class 'builtin_function_or_method'>
tolist <class 'builtin_function_or_method'>
tostring <class 'builtin_function_or_method'>
tounicode <class 'builtin_function_or_method'>
typecode <class 'str'>
type(ary)=<class 'array.array'>
ary=array('d', [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])

__add__ <class 'method-wrapper'>
__class__ <class 'type'>
__contains__ <class 'method-wrapper'>
__copy__ <class 'builtin_function_or_method'>
__deepcopy__ <class 'builtin_function_or_method'>
__delattr__ <class 'method-wrapper'>
__delitem__ <class 'method-wrapper'>
__dir__ <class 'builtin_function_or_method'>
__doc__ <class 'str'>
__eq__ <class 'method-wrapper'>
__format__ <class 'builtin_function_or_method'>
__ge__ <class 'method-wrapper'>
__getattribute__ <class 'method-wrapper'>
__getitem__ <class 'method-wrapper'>
__gt__ <class 'method-wrapper'>
__hash__ <class 'NoneType'>
__iadd__ <class 'method-wrapper'>
__imul__ <class 'method-wrapper'>
__init__ <class 'method-wrapper'>
__init_subclass__ <class 'builtin_function_or_method'>
__iter__ <class 'method-wrapper'>
__le__ <class 'method-wrapper'>
__len__ <class 'method-wrapper'>
__lt__ <class 'method-wrapper'>
__mul__ <class 'method-wrapper'>
__ne__ <class 'method-wrapper'>
__new__ <class 'builtin_function_or_method'>
__reduce__ <class 'builtin_function_or_method'>
__reduce_ex__ <class 'builtin_function_or_method'>
__repr__ <class 'method-wrapper'>
__rmul__ <class 'method-wrapper'>
__setattr__ <class 'method-wrapper'>
__setitem__ <class 'method-wrapper'>
__sizeof__ <class 'builtin_function_or_method'>
__str__ <class 'method-wrapper'>
__subclasshook__ <class 'builtin_function_or_method'>
append <class 'builtin_function_or_method'>
buffer_info <class 'builtin_function_or_method'>
byteswap <class 'builtin_function_or_method'>
count <class 'builtin_function_or_method'>
extend <class 'builtin_function_or_method'>
frombytes <class 'builtin_function_or_method'>
fromfile <class 'builtin_function_or_method'>
fromlist <class 'builtin_function_or_method'>
fromstring <class 'builtin_function_or_method'>
fromunicode <class 'builtin_function_or_method'>
index <class 'builtin_function_or_method'>
insert <class 'builtin_function_or_method'>
itemsize <class 'int'>
pop <class 'builtin_function_or_method'>
remove <class 'builtin_function_or_method'>
reverse <class 'builtin_function_or_method'>
tobytes <class 'builtin_function_or_method'>
tofile <class 'builtin_function_or_method'>
tolist <class 'builtin_function_or_method'>
tostring <class 'builtin_function_or_method'>
tounicode <class 'builtin_function_or_method'>
typecode <class 'str'>

脚註 編集

  1. ^ 3.10.1 Documentation » The Python Standard Library » Data Types » array — Efficient arrays of numeric values” (2021年12月16日). 2021年12月16日閲覧。