Pythonslice ()関数
例
タプルとスライスオブジェクトを作成します。スライスオブジェクトを使用して、タプルの最初の2つのアイテムのみを取得します。
a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(2)
print(a[x])
定義と使用法
このslice()
関数はスライスオブジェクトを返します。
スライスオブジェクトは、シーケンスをスライスする方法を指定するために使用されます。スライスを開始する場所と終了する場所を指定できます。ステップを指定することもできます。これにより、たとえば、他のすべてのアイテムのみをスライスできます。
構文
slice(start, end, step)
パラメータ値
Parameter | Description |
---|---|
start | Optional. An integer number specifying at which position to start the slicing. Default is 0 |
end | An integer number specifying at which position to end the slicing |
step | Optional. An integer number specifying the step of the slicing. Default is 1 |
その他の例
例
タプルとスライスオブジェクトを作成します。位置3でスライスオブジェクトを開始し、位置5にスライスして、結果を返します。
a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(3,
5)
print(a[x])
例
タプルとスライスオブジェクトを作成します。stepパラメーターを使用して、3つおきの項目を返します。
a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(0,
8, 3)
print(a[x])