[PYTHON] Convert a slice object to a list of index numbers

When working with a range of Python lists

int_list[10:2]

It is a slice object that is actually passed to the __get__ method, but when you try to implement a list class by yourself, it is a problem if you try to correspond to the slice argument. There is no method to convert slice to a column of index numbers.

It's hard to write such a conversion process on your own, if step is a negative number / None, if start or stop is a negative number / None, this will die in a storm of conditional branching.

However, there are methods that do such troublesome processing inconspicuously. It is slice.indices (). If you give the length of the list, it will normalize the negative number / None to a proper index number accordingly. With this, it's relatively easy to get a column of index numbers.

def iterate_by_slice(s: slice, length: int):
    i, stop, step = s.indices(length)
    if step > 0 :
        while i < stop:
            yield i
            i += step
    else:
        while i > stop:
            yield i
            i += step

Please make more and more list-like classes that are useful to everyone.

Recommended Posts

Convert a slice object to a list of index numbers
[Python] How to convert a 2D list to a 1D list
I tried to create a list of prime numbers with python
String conversion of a list containing numbers
Display output of a list of floating point numbers
Convert a multidimensional list (array) to one dimension
How to convert a class object to a dictionary with SQLAlchemy
How to write a list / dictionary type of Python3
Python: Get a list of methods for an object
[Python] Convert list to Pandas [Pandas]
[Python] How to make a list of character strings character by character
How to shuffle a part of a Python list (at random.shuffle)
[Command] Command to get a list of files containing double-byte characters
How to display a list of installable versions with pyenv
How to get a list of built-in exceptions in python
How to get a list of links from a page from wikipedia
Is there a secret to the frequency of pi numbers?
How to connect the contents of a list into a string
Get the value of a specific key up to the specified index in the dictionary list in Python
Convert Tweepy Status object to JSON
Extract the index of the original set list that corresponds to the list of subsets.
I tried to get the index of the list using the enumerate function
Convert A4 PDF to A3 every 2 pages
Try to get a list of breaking news threads in Python.
Convert list to DataFrame with python
Convert PDF of Go To Eat Hokkaido campaign dealer list to CSV
Python> list> Convert double list to single list
Convert a large number of PDF files to text files using pdfminer
I tried to get a list of AMI Names using Boto3
Convert a string to an image
[Python] Convert natural numbers to ordinal numbers
How to get a list excluding elements whose index is i ...?
A light introduction to object detection
Convert decimal numbers to n-ary numbers [python]
A tool to convert Juniper config
Generate a list of consecutive characters
[Python] A program that rotates the contents of the list to the left
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
[Python] How to put any number of standard inputs in a list
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Linux] Command to get a list of commands executed in the past
A function that generates a list with up to 3 prime numbers as elements
I measured 6 methods to get the index of the maximum value (minimum value) of the list
I want to sort a list in the order of other lists
Convert PDF of Kumamoto Prefecture Go To EAT member store list to CSV
How to format a list of dictionaries (or instances) well in Python
[Django] Convert QuerySet to dictionary type list
Try to make a kernel of Jupyter
Display a list of alphabets in Python 3
python> Convert tuple to list> aList = list (pi_tuple)
[python] Get a list of instance variables
Summary of how to use Python list
[Python] Get a list of folders only
An Introduction to Object-Oriented-Give an object a child.
Convert PDF of Chiba Prefecture Go To EAT member store list to CSV (command)
How to pass the execution result of a shell command in a list in Python
Convert PDF of product list containing effective surfactants for new coronavirus to CSV
How to achieve something like a list of void * (or variant) in Go?
[Python] Concatenate a List containing numbers and write it to an output file.
Convert PDF of list of Go To EAT member stores in Niigata prefecture to CSV
I want to see a list of WebDAV files in the Requests module