About python comprehension

Introduction

There is a format called comprehension in python, I've been doing it without using it at all, but it's related to performance, so I thought I'd try to understand it properly.

Make a list

Make a list of only odd numbers from 1 to 100. How to make without using comprehension is as follows

list1.py


list = []
for number in range(1,101):
    if number % 2 ==1:
        list.append(number)
print(list)

Next, I'll try the same thing using comprehensions. The program is as follows

list2.py


list = [number for number in range(1,101) if number %2 ==1]
print(list)

The first number will be the number stored in the list. The number after for is a part of the number for for.

Make a set

Make a set of only odd numbers from 1 to 99. Call it in a comprehension like a list.

shugo.py


list = {number for number in range(1,101) if number %2 ==1}
print(list)

Make a dictionary

Similarly, dictionaries use comprehensions. A program that counts how many times the word character appears

jisyo.py


word = 'aiueokakiku'
word_count = {x:word.count(x) for x in word}
print(word_count)

About tuples

Let's make it like a list.

tuple1.py


tuple = (number for number in range(1,101) if number %2 ==1)
print(tuple)

When you run <generator object <genexpr> at 0x101435f61> It's not an error, but the generator returns when I use () In other words, ** tuples have no inclusions **.

Performance comparison

I will try if it is really fast. First, let's make a list of odd numbers from 1 to 10000000 without using comprehension.

test1.py


import time
start = time.time()

list = []
for number in range(1,10000001):
    if number % 2 ==1:
        list.append(number)

print("Execution time:{0}",time.time() - start)

The average of 5 runs was ** 1.71131701469 **

Next, I will try the same thing with the inclusion notation

test2.py


import time
start = time.time()

list = [number for number in range(1,10000001) if number %2 ==1]

print("Execution time:{0}",time.time() - start)

The average of 5 runs was ** 0.88413858413 **

result

Writing in comprehension is about twice as fast and simply reduces the amount of code. It seems that it will be easier to read as you get used to it, so I will do my best to get used to it as soon as possible.

Recommended Posts

About python comprehension
Python comprehension
Python comprehension
About python slices
About Python tqdm.
About python yield
About python, class
About python inheritance
About python, range ()
About python decorators
Python> Comprehension / Comprehension> List comprehension
About python reference
About Python decorators
[Python] About multi-process
About Python for loops
Summary about Python scraping
About function arguments (python)
Python list comprehension speed
[Python] Memo about functions
Summary about Python3 + OpenCV3
About Python, for ~ (range)
About Python3 character code
[Python] Memo about errors
About Python development environment
Python: About function arguments
Python, about exception handling
About Python Pyramid traversal
About Python3 ... (Ellipsis object)
[Python] Chapter 01-01 About Python (First Python)
[Python] About standard input
About __all__ in python
[Python] Find out about pip
About Fabric's support for Python 3
Python
About python objects and classes
About Python variables and objects
About the Python module venv
Think about architecture in python
About python beginner's memorandum function
About the ease of Python
About the enumerate function (python)
About various encodings of Python 3
About Python, len () and randint ()
About Perl, Python, PHP, Ruby
About Python datetime and timezone
A memorandum about correlation [Python]
A memorandum about Python mock
About Python string comparison operators
About Python and regular expressions
About the features of Python
About "for _ in range ():" in python
About Python and os operations
Python # About reference and copy
About Python sort () and reverse ()
A note about [python] __debug__
Python Note: About comparison using is
About installing Pwntools and Python2 series
Python: A Note About Classes 1 "Abstract"
[Python] Let's write briefly about comprehensions
About python dict and sorted functions
About dtypes in Python and Cython