Python/array

出典: フリー教科書『ウィキブックス(Wikibooks)』

Array[編集]

array モジュールは、同一の型の要素をもつシーケンスを提供します[1]。 JavaScript の TypedArray や、C言語等の静的型付けの言語の配列に相当します。

コンストラクター[編集]

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日閲覧。