シーケンス

編集

Pythonでは、複数のオブジェクトを要素として持つオブジェクト(=コレクション)のうち、整数のインデックスによって要素を参照できるオブジェクトのグループをシーケンス(sequence)と呼びます[1]。 組込み型の代表的なシーケンスは、リストタプルレンジ オブジェクトです[2]

スライス記法

編集

シーケンスに共通の機能として、スライス記法があります。

スライス記法
for li in (
    [i for i in range(10)],
    (0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
    range(10),
    "0123456789",
    b"0123456789",
):
    print(
        f"""\
{type(li)=}
{li=}
{li[2:]=}
{li[2:6]=}
{li[:6]=}
{li[:]=}
{id(li)=},{id(li[:])=}
{id(li)=},{id(li[:9])=}
{li[1:8:2]=}
{li[1::2]=}
"""
    )
実行結果
type(li)=<class 'list'>
li=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
li[2:]=[2, 3, 4, 5, 6, 7, 8, 9]
li[2:6]=[2, 3, 4, 5]
li[:6]=[0, 1, 2, 3, 4, 5]
li[:]=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
id(li)=23247490610496,id(li[:])=23247489732224
id(li)=23247490610496,id(li[:9])=23247489732224
li[1:8:2]=[1, 3, 5, 7]
li[1::2]=[1, 3, 5, 7, 9]

type(li)=<class 'tuple'>
li=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
li[2:]=(2, 3, 4, 5, 6, 7, 8, 9)
li[2:6]=(2, 3, 4, 5)
li[:6]=(0, 1, 2, 3, 4, 5)
li[:]=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
id(li)=23247491653056,id(li[:])=23247491653056
id(li)=23247491653056,id(li[:9])=23247490368448
li[1:8:2]=(1, 3, 5, 7)
li[1::2]=(1, 3, 5, 7, 9)

type(li)=<class 'range'>
li=range(0, 10)
li[2:]=range(2, 10)
li[2:6]=range(2, 6)
li[:6]=range(0, 6)
li[:]=range(0, 10)
id(li)=23247491019616,id(li[:])=23247490390464
id(li)=23247491019616,id(li[:9])=23247490390464
li[1:8:2]=range(1, 8, 2)
li[1::2]=range(1, 10, 2)

type(li)=<class 'str'>
li='0123456789'
li[2:]='23456789'
li[2:6]='2345'
li[:6]='012345'
li[:]='0123456789'
id(li)=23247489812400,id(li[:])=23247489812400
id(li)=23247489812400,id(li[:9])=23247489814704
li[1:8:2]='1357'
li[1::2]='13579'

type(li)=<class 'bytes'>
li=b'0123456789'
li[2:]=b'23456789'
li[2:6]=b'2345'
li[:6]=b'012345'
li[:]=b'0123456789'
id(li)=23247491020288,id(li[:])=23247491020288
id(li)=23247491020288,id(li[:9])=23247490390224
li[1:8:2]=b'1357'
li[1::2]=b'13579'

シーケンス・オブジェクトごとのアトリビュートの有無

編集

組込み関数hasattr(obj, name)を使うと、オブジェクトobjが名前nameな属性を持っているかの検査ができます。

シーケンス・オブジェクトごとのアトリビュートの有無
sequences = (
    [i for i in range(10)],
    (0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
    range(10),
    "0123456789",
    b"0123456789",
    zip([],[]),
)
attrs = set()

for li in sequences:
    for x in dir(li):
        attrs.add(x)

c = 0
for x in sorted(attrs):
    if c % 10 == 0:
        print("Type", ", ".join(str(type(li)) for li in sequences), sep=": ")
    print(x, ", ".join("o" if hasattr(li, x) else "x" for li in sequences), sep=": ")
    c += 1
else:
    print("Type", ", ".join(str(type(li)) for li in sequences), sep=": ")
実行結果
Type: <class 'list'>, <class 'tuple'>, <class 'range'>, <class 'str'>, <class 'bytes'>, <class 'zip'>
__add__: o, o, x, o, o, x
__bool__: x, x, o, x, x, x
__class__: o, o, o, o, o, o
__contains__: o, o, o, o, o, x
__delattr__: o, o, o, o, o, o
__delitem__: o, x, x, x, x, x
__dir__: o, o, o, o, o, o
__doc__: o, o, o, o, o, o
__eq__: o, o, o, o, o, o
__format__: o, o, o, o, o, o
Type: <class 'list'>, <class 'tuple'>, <class 'range'>, <class 'str'>, <class 'bytes'>, <class 'zip'>
__ge__: o, o, o, o, o, o
__getattribute__: o, o, o, o, o, o
__getitem__: o, o, o, o, o, x
__getnewargs__: x, o, x, o, o, x
__gt__: o, o, o, o, o, o
__hash__: o, o, o, o, o, o
__iadd__: o, x, x, x, x, x
__imul__: o, x, x, x, x, x
__init__: o, o, o, o, o, o
__init_subclass__: o, o, o, o, o, o
Type: <class 'list'>, <class 'tuple'>, <class 'range'>, <class 'str'>, <class 'bytes'>, <class 'zip'>
__iter__: o, o, o, o, o, o
__le__: o, o, o, o, o, o
__len__: o, o, o, o, o, x
__lt__: o, o, o, o, o, o
__mod__: x, x, x, o, o, x
__mul__: o, o, x, o, o, x
__ne__: o, o, o, o, o, o
__new__: o, o, o, o, o, o
__next__: x, x, x, x, x, o
__reduce__: o, o, o, o, o, o
Type: <class 'list'>, <class 'tuple'>, <class 'range'>, <class 'str'>, <class 'bytes'>, <class 'zip'>
__reduce_ex__: o, o, o, o, o, o
__repr__: o, o, o, o, o, o
__reversed__: o, x, o, x, x, x
__rmod__: x, x, x, o, o, x
__rmul__: o, o, x, o, o, x
__setattr__: o, o, o, o, o, o
__setitem__: o, x, x, x, x, x
__sizeof__: o, o, o, o, o, o
__str__: o, o, o, o, o, o
__subclasshook__: o, o, o, o, o, o
Type: <class 'list'>, <class 'tuple'>, <class 'range'>, <class 'str'>, <class 'bytes'>, <class 'zip'>
append: o, x, x, x, x, x
capitalize: x, x, x, o, o, x
casefold: x, x, x, o, x, x
center: x, x, x, o, o, x
clear: o, x, x, x, x, x
copy: o, x, x, x, x, x
count: o, o, o, o, o, x
decode: x, x, x, x, o, x
encode: x, x, x, o, x, x
endswith: x, x, x, o, o, x
Type: <class 'list'>, <class 'tuple'>, <class 'range'>, <class 'str'>, <class 'bytes'>, <class 'zip'>
expandtabs: x, x, x, o, o, x
extend: o, x, x, x, x, x
find: x, x, x, o, o, x
format: x, x, x, o, x, x
format_map: x, x, x, o, x, x
fromhex: x, x, x, x, o, x
hex: x, x, x, x, o, x
index: o, o, o, o, o, x
insert: o, x, x, x, x, x
isalnum: x, x, x, o, o, x
Type: <class 'list'>, <class 'tuple'>, <class 'range'>, <class 'str'>, <class 'bytes'>, <class 'zip'>
isalpha: x, x, x, o, o, x
isascii: x, x, x, o, o, x
isdecimal: x, x, x, o, x, x
isdigit: x, x, x, o, o, x
isidentifier: x, x, x, o, x, x
islower: x, x, x, o, o, x
isnumeric: x, x, x, o, x, x
isprintable: x, x, x, o, x, x
isspace: x, x, x, o, o, x
istitle: x, x, x, o, o, x
Type: <class 'list'>, <class 'tuple'>, <class 'range'>, <class 'str'>, <class 'bytes'>, <class 'zip'>
isupper: x, x, x, o, o, x
join: x, x, x, o, o, x
ljust: x, x, x, o, o, x
lower: x, x, x, o, o, x
lstrip: x, x, x, o, o, x
maketrans: x, x, x, o, o, x
partition: x, x, x, o, o, x
pop: o, x, x, x, x, x
remove: o, x, x, x, x, x
replace: x, x, x, o, o, x
Type: <class 'list'>, <class 'tuple'>, <class 'range'>, <class 'str'>, <class 'bytes'>, <class 'zip'>
reverse: o, x, x, x, x, x
rfind: x, x, x, o, o, x
rindex: x, x, x, o, o, x
rjust: x, x, x, o, o, x
rpartition: x, x, x, o, o, x
rsplit: x, x, x, o, o, x
rstrip: x, x, x, o, o, x
sort: o, x, x, x, x, x
split: x, x, x, o, o, x
splitlines: x, x, x, o, o, x
Type: <class 'list'>, <class 'tuple'>, <class 'range'>, <class 'str'>, <class 'bytes'>, <class 'zip'>
start: x, x, o, x, x, x
startswith: x, x, x, o, o, x
step: x, x, o, x, x, x
stop: x, x, o, x, x, x
strip: x, x, x, o, o, x
swapcase: x, x, x, o, o, x
title: x, x, x, o, o, x
translate: x, x, x, o, o, x
upper: x, x, x, o, o, x
zfill: x, x, x, o, o, x
Type: <class 'list'>, <class 'tuple'>, <class 'range'>, <class 'str'>, <class 'bytes'>, <class 'zip'>

脚註

編集
  1. ^ sequenceと言う型があるわけではありません。
  2. ^ 辞書も3.6以降では順序固定ですが、シーケンスの一種だと考えることは稀です。