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.
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 ... 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.)
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.
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