[PYTHON] EP 11 Use `zip` to Process Iterators in Parallel

  • The zip built-in function can be used to iterate over multiple iterators in parallel.

Effective Python

Verbose

>>> names = "Michel Stonbreaker Peter Bilas Franklin".split()
>>> letters = [len(n) for n in names]
>>> longest_name = None
>>> max_letters = 0
>>> for i in range(len(names)):
...  count = letters[i]
...  if count > max_letters:
...   longest_name = names[i]
...   max_letters = count
...
>>> print(longest_name)
Stonbreaker

You can use enumerate. But it is still verbose.

>>> for i, name in enumerate(names):
...  count = letters[i]
...  if count > max_letters:
...   longest_name = names[i]
...   max_letters = count
...
>>> print(longest_name)
Stonbreaker
>>> for name, count in zip(names, letters):
...  if count > max_letters:
...   longest_name = name
...   max_letters = count
...
>>> print(longest_name)
Stonbreaker

zip truncate implecitly. if it iterate the different length of iteratable objects.

itertools.zip_longest could be a solution for this.

>>> for name, count in zip(names, letters):
...     print(name, count)
...
Michel 6
Stonbreaker 11
Peter 5
Bilas 5
Franklin 8
>>> from itertools import zip_longest
>>> for name, count in zip_longest(names, letters):
...  print(name, count)
...
Michel 6
Stonbreaker 11
Peter 5
Bilas 5
Franklin 8
Matei None
>>>

Recommended Posts

EP 11 Use `zip` to Process Iterators in Parallel
How to use the zip function
How to use classes in Theano
Mock in python-how to use mox
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
How to use python zip function
[Introduction to Python] How to use class in Python?
How to use Google Test in C
Easy way to use Wikipedia in Python
Minimum knowledge to use Form in Flask
How to use Anaconda interpreter in PyCharm
How to use __slots__ in Python class
How to use Python zip and enumerate
How to use regular expressions in Python
How to use Map in Android ViewPager
How to write this process in Perl?
How to use is and == in Python
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
Use cryptography module to handle OpenSSL in Python
How to use tkinter with python in pyenv
Use ELMo, BERT, USE to detect anomalies in sentences
Use pygogo to get the log in json.
Use os.getenv to get environment variables in Python
EP 24 Use `@ classmethod` Polymorphism to Construct Objects Generically
[For beginners] How to use say command in python!
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
Fatal error in launcher: Unable to create process using'"'
A memorandum on how to use keras.preprocessing.image in Keras
Convert the image in .zip to PDF with Python
Fatal error in launcher: Unable to create process using'"'
How to use bootstrap in Django generic class view
How to use template engine in pyramid 1 file application
How to use the exists clause in Django's queryset
Reasons to use long type in SQLite3 (C # Mono.Data.Sqlite)
How to use variables in systemd Unit definition files
Convenient to use matplotlib subplots in a for statement
I tried to summarize how to use pandas in python
How to use jupyter lab in Windows 10 local environment
How to use the model learned in Lobe in Python
How to use Decorator in Django and how to make it
Force luigi to do parallel processing in windows environment
Use date to x-axis of tsplot depicted in seaborn
How to use Spacy Japanese model in Google Colaboratory
I want to use the R dataset in python
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
Parallel processing of Python joblib does not work in uWSGI environment. How to process in parallel on uWSGI?