Ciao ... †
Sometimes you want to combine nested (nested) lists in Python into a single list, that is, flatten it. Information on how to deal with such cases can be found immediately by searching the Web. But isn't it annoying to copy and paste it one by one? I'm annoyed. So, in this article, I will introduce the module flati
that flattens in Python.
Currently it supports Python 2.7 and Python 3.4 ~ 3.8. Since it is a Pure-Python module, it will work on various platforms such as Windows / macOS / Ubuntu as long as it has a compatible Python environment.
I named it flati
by applying flatten processing and Fulvio Frati (a teacher who has studied with me). Please pronounce "Furate".
$ pip install flati
or
$ python -m pip install flati
import flati
# flati.flatten()Is a generator that returns nested Iterable objects element by element.
#If you want to combine the returned values into one Iterable object, list()Please use etc.
iterable = [(1, 2, 3), (4, (5, 6))]
list(flati.flatten(iterable))
# => [1, 2, 3, 4, 5, 6]
#I will write it again. flati.flatten()Is a generator.
import types
isinstance(flati.flatten(iterable), types.GeneratorType)
# => True
#If you don't want to flatten a particular Iterable object,"ignore"Please specify with an argument.
iterable = [('abc'), ('def', ('g', 'hi'))]
list(flati.flatten(iterable, ignore=str))
# => ['abc', 'def', 'g', 'hi']
Tips
If you want to flatten NumPy's ndarray
, use the NumPy function. That is faster.
numpy.ravel ()
. Personally, this is the most recommended.ndarray
.Reference: https://python.atelierkobato.com/flat/
I will introduce what I found by searching for "flatten" with PyPI. Those that flatten the dict
type are excluded.
str
and bytes
types are hard-coded so that they are not flattened.argument, it may be a problem when you do not want to flatten only
str`.list
type is supported. Release has stopped in December 2018. The Documentation (https://flattenit.readthedocs.io/en/latest/) is solid.I'm tired so I'll leave it around here.
--You don't have to copy the code that comes out by searching the Web --Any Iterable object is OK --ʻIgnore` You can specify the type you do not want to flatten with the argument --Since it is a generator, memory usage is easy when handling huge data. --The number of characters in the module name is small (5 characters) --Simple implementation (source code) --Pure-Python module, so feel free to run on various platforms --Issue and Pull request for request and bug report / fix can be in Japanese (title should be in English) --Click here for details: https://github.com/ikegami-yukino/flati/blob/master/CONTRIBUTING.md
――If you like it, please give a star to flati's GitHub repository. With just one click, my development motivation is up. ――Stars are not enough! If you like, you can send me money or things by pressing the Sponsor button on flati's GitHub repository. The motivation for my development will increase considerably.
For the next few days, I would like to introduce modules that I made for the time being but did not advertise at all.
Shiver and wait ... †
Recommended Posts