Python comprehension (list and generator expressions) [additional]

Introduction

I think Python's comprehension is famous (?) As a unique Python writing style. This time, I will summarize the basic part of such inclusion notation. Specifically, it is about list comprehensions and generator expressions.

List comprehension

I think the list comprehension is especially famous.

python


print([i for i in range(0, 10)])

This alone will output a list containing numbers from 0 to 9.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

It's very easy to make. Of course, you can do more.

python


print('¥n'.join(['FizzBuzz' if i%15 == 0 else
                 'Fizz' if i%3 == 0 else
                 'Buzz' if i%5 == 0 else
                 str(i) for i in range(1, 101)]))

This is FizzBuzz. The first thing to do when you start learning a new language. Numbers from 1 to 100 are displayed in order, Fizz is displayed for multiples of 3, Buzz is displayed for multiples of 5, and FizzBuzz is displayed for multiples of 3 and 5 instead of numbers. By the way, when executed, 1 to 100 are displayed one by one with line breaks. That is the part of'\ n'.join.

I mean, it's something I write myself, but it's a pretty aggressive code ... This is, next time I will introduce a better way of writing, so I dare to write it like this. Let's go next.

Generator type

Generator type ... Difficult words have appeared ... To elaborate on what a generator expression is, we must explain what a generator is, and to explain a generalta we must explain an iterator, and then we explain an iterator qs. Wow drftgy Fujiko lp; @: (I may post it as another article next time)

Simply put, it's a handy guy who can quickly create a function in parentheses and use it quickly.

python


print(i for i in range(0,10))

It feels almost the same as the list comprehension notation. The difference is that there is no []. At this time, how is it output?

<generator object <genexpr> at 0x10d747b48>

Click here for output. It's completely different from the previous list! This doesn't make any sense, so don't forget [] when writing in comprehensions.

However, there are quite a few times when this generator formula is convenient.

print('¥n'.join((i%3 == 0)*'Fizz' + (i%5 == 0)*'Buzz' or str(i) for i in range(1, 101)))

Yes. I feel that the amount of information in one line is quite large, but some code was written in one line. As you can guess, this is FizzBuzz. The output will be in exactly the same format as before.

You can't do this with list comprehension! (Isn't it?) You can force the code above the list comprehension into one line, but ... I hate it being hard to see. (Since both list comprehension and generator expressions exceed 80 characters, line breaks are actually required.)

Impressions

This time we introduced only two types, but it seems that you can do quite a lot with this too! Above all, I like the inclusion notation because it looks like Pythonic code. After that, I think that it is better to actively use the place where the generator type can be used. If it is a list comprehension notation, it will make a list one by one.

that's all.

Postscript (2015/12/2)

print('¥n'.join((i%3 == 0)*'Fizz' + (i%5 == 0)*'Buzz' or str(i) for i in range(1, 101)))

You can't do this in list comprehension! (Isn't it?)

I'm sorry, I can.

print('¥n'.join([(i%3 == 0)*'Fizz' + (i%5 == 0)*'Buzz' or str(i) for i in range(1, 101)]))

You will still get exactly the same output.

I made a big mistake that I shouldn't make a mistake here, and I even feel like the purpose of this article has been completely lost ... I'll be careful in future.

Recommended Posts

Python comprehension (list and generator expressions) [additional]
Python tuple comprehensions and generator expressions
Python> Comprehension / Comprehension> List comprehension
Python list comprehension speed
Python list and tuples and commas
Python list comprehensions and generators
About Python and regular expressions
Difference between list () and [] in Python
Introductory Python Modules and conditional expressions
Python comprehension
List comprehension
Python comprehension
List comprehension
[Python] list
Overlapping regular expressions in Python and Java
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
Python basic operation 1st: List comprehension notation
difference between statements (statements) and expressions (expressions) in Python
Python data structure and internal implementation ~ List ~
Difference between append and + = in Python list
[Python Iroha] Difference between List and Tuple
Effective Python memo Item 7 Use list comprehension instead of map and filter
I measured the speed of list comprehension, for and while with python2.7.
[Python] List Comprehension Various ways to create a list
Python basics: list
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
[Python] Generator function
list and sum
list and numpy
[Python] Regular Expressions Regular Expressions
List of Python code to move and remember
Note: List comprehension
Python list manipulation
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
filter, map, reduce with js and python (There are also arrow expressions, lambda expressions, comprehension expressions)
[Python] AGC043A (Problem reading comprehension and DP) [At Coder]
Mayungo's Python Learning Note: List of stories and links
Implemented List and Bool in Python and SQLite3 (personal note)
Writing using lambda expressions and filter functions and writing using list comprehensions
Implementing a generator using Python> link> yield and next ()> yield
list comprehension because operator.methodcaller cannot be used in python 2.5
[Python] How to sort dict in list and instance in list
Python3 comprehension (List, dictionary) that I have seen somewhere
[python] Compress and decompress
List of python modules
Python> list> extend () or + =
Python and numpy tips
[Python] pip and wheel
FizzBuzz in list comprehension
Batch design and python
Python iterators and generators
Filter List in Python
Python packages and modules
Vue-Cli and Python integration
python unittest assertXXX list
Ruby, Python and map
Generator comprehension notation Tuple comprehension notation
Python3 List / dictionary memo
[Memo] Python3 list sort
python input and output
OpenCV3 Python API list