[PYTHON] 100 amateur language processing knocks: 02

It is a challenge record of Language processing 100 knock 2015. Please see the list from here.

Environment change again

I have only asked two questions yet, but decided to change the environment again. Change Python to 3.x.

I was hesitant to install Python 3.x because it seemed to be a problem, but I asked _ha1f to teach Anaconda in Previous question. I could easily make it 3.5.2. Thank you!

Anaconda is like an assortment of commonly used packages. You can find a lot of explanations by google, but for installation, [Installing Python using Anaconda (Ubuntu Linux)](http://pythondatascience.plavox.info/Installing python / anaconda-ubuntu-linux /) is easy to understand. It is.

Chapter 1: Preparatory movement

02. "Police car" + "Taxi" = "Patatokukashi"

Obtain the character string "Patatokukashi" by alternately connecting the characters "Police car" + "Taxi" from the beginning.

The finished code:

main.py


# coding: utf-8
target1 = 'Police car'
target2 = 'taxi'
result = ''
for (a, b) in zip(target1, target2):
	result += a + b
print(result)

main2.py


# coding: utf-8
from functools import reduce

target1 = 'Police car'
target2 = 'taxi'
result = ''.join(reduce(lambda x, y: x + y, zip(target1, target2)))
print(result)

Execution result:

Terminal


Patatoku Kashii

About zip () and ʻitertools.zip_longest ()`

zip () creates a list of elements from multiple iterables (lists, tuples, etc.). It's amazing that Python has such a useful function!

I will try various things.

Interpreter


>>> target1 = 'abcde'
>>> target2 = '12345'
>>> zip(target1, target2)
<zip object at 0x7f1294d76108>

that? Did you have a zip object?

When I looked it up, it seems that Python 2.x returned a list, but 3.x returned an iterator. It may have become more efficient as it started processing little by little from the front (this kind of thing seems to be called a generator). Always be aware of the version when looking at Python documentation and commentary!

Since it is an iterator, you can check the contents by converting it to a list.

Interpreter 1


>>> target1 = 'abcde'
>>> target2 = '12345'
>>> res_zip = zip(target1, target2)
>>> list(res_zip)
[('a', '1'), ('b', '2'), ('c', '3'), ('d', '4'), ('e', '5')]

Each element is retrieved one by one and packed into a tuple. This is the flow of taking out one by one.

If the original number of elements is different, it will be adjusted to the smaller one.

Interpreter 2


>>> target1 = 'abc'
>>> target2 = '12345'
>>> res_zip = zip(target1, target2)
>>> list(res_zip)
[('a', '1'), ('b', '2'), ('c', '3')]

If you want to match the longer one, you can use ʻitertools.zip_longest () `.

Interpreter 3


>>> import itertools
>>> res_zip = itertools.zip_longest(target1, target2)
>>> list(res_zip)
[('a', '1'), ('b', '2'), ('c', '3'), (None, '4'), (None, '5')]

If you don't have enough elements, None will be entered, but you can also specify it.

Interpreter 4


>>> res_zip = itertools.zip_longest(target1, target2, fillvalue = 'zzz')
>>> list(res_zip)
[('a', '1'), ('b', '2'), ('c', '3'), ('zzz', '4'), ('zzz', '5')]

Auctioned auction.

Go back to zip () and try sticking 3 or more together.

Interpreter 5


>>> target1 = 'abcde'
>>> target2 = '12345'
>>> target3 = 'AIUEO'
>>> res_zip = zip(target1, target2, target3)
>>> list(res_zip)
[('a', '1', 'Ah'), ('b', '2', 'I'), ('c', '3', 'U'), ('d', '4', 'e'), ('e', '5', 'O')]

The result is as expected.

Returns the result of zip ()

If you pass the result of zip () to zip () again, you can get it back. First, use zip () to pass 3 lists and make 5 tuples.

Interpreter 6


>>> target1 = 'abcde'
>>> target2 = '12345'
>>> target3 = 'AIUEO'
>>> res_zip = zip(target1, target2, target3)
>>> list(res_zip)
[('a', '1', 'Ah'), ('b', '2', 'I'), ('c', '3', 'U'), ('d', '4', 'e'), ('e', '5', 'O')]

If you pass these 5 tuples to zip () again, this time you will take each element from the 5 tuples to make a tuple, so you will return to the original 3 tuples. I see!

Interpreter 7


>>> res_zip = zip(target1, target2, target3)
>>> res_list = list(res_zip)
>>> res2_zip = zip(res_list[0], res_list[1], res_list[2], res_list[3], res_list[4])
>>> list(res2_zip)
[('a', 'b', 'c', 'd', 'e'), ('1', '2', '3', '4', '5'), ('Ah', 'I', 'U', 'e', 'O')]

came back! It is a tuple instead of a string, but how to return it will be described later.

The argument of zip () is variable, but if you add \ * before the iterable, it will be separated into the arguments. Therefore, you don't have to write res_list [0], res_list [1] ... one by one, but * res_list is OK. Convenient!

Interpreter 8


>>> res_zip = zip(target1, target2, target3)
>>> res_list = list(res_zip)
>>> res2_zip = zip(*res_list)
>>> list(res2_zip)
[('a', 'b', 'c', 'd', 'e'), ('1', '2', '3', '4', '5'), ('Ah', 'I', 'U', 'e', 'O')]

Return tuple to string

Interpreter 9


>>> chars_tuple = ('a', 'b', 'c', 'd', 'e')
>>> ''.join(chars_tuple)
'abcde'

About functools.reduce ()

Interpreter 10


>>> def add(x, y):
...     return x + y
... 
>>> import functools
>>> functools.reduce(add, [1, 2, 3, 4, 5])
15

functools.reduce () passes the previous result and the value taken from the iterable to the function to create a new result, which is repeated until the end of the iterable. The functools.reduce (add, [1, 2, 3, 4, 5]) in the example above will do something like the following code.

Interpreter 11


>>> result = 0
>>> for x in [1, 2, 3, 4, 5]:
...     result = add(result, x)
... 
>>> result
15

The initial value of the result corresponding to result = 0 can be specified by the third argument offunctools.reduce (). If omitted, two will be extracted from the iterable only for the first time, and the result of passing them to the function will be the initial value.

In the second solution example main2.py, the lambda expression lambda x, y: x + y is specified in the function part. For lambda expressions, [100 amateur language processing knocks: 18](https://qiita.com/segavvy/items/adee520db1a257e347d5#%E3%83%A9%E3%83%A0%E3%83%80%E5 % BC% 8F) explains a little, so please refer to that.

I was addicted to the state transition of the iterator

What I was addicted to trying this time is that once an iterator is converted to a list, it can no longer be used (iterators that have advanced cannot be reset).

For example, if you run interpreter 7 immediately after running interpreter 6 above, the first line of interpreter 7 res_zip = zip (target1, target2, target3) looks unnecessary at first glance. It's already running on line 4 of interpreter 6. However, when converting to a list with the 5th line list (res_zip) of interpreter 6, the iterator of res_zip advances, so even if you list (res_zip) again, it will be an empty list.

If you're used to Python, you might be able to care about iterator state transitions naturally, but I was addicted to it for a while.   That's all for the third knock. If you have any mistakes, I would appreciate it if you could point them out.

Recommended Posts

100 amateur language processing knocks: 41
100 amateur language processing knocks: 56
100 amateur language processing knocks: 24
100 amateur language processing knocks: 50
100 amateur language processing knocks: 59
100 amateur language processing knocks: 70
100 amateur language processing knocks: 62
100 amateur language processing knocks: 92
100 amateur language processing knocks: 06
100 amateur language processing knocks: 81
100 amateur language processing knocks: 46
100 amateur language processing knocks: 88
100 amateur language processing knocks: 89
100 amateur language processing knocks: 43
100 amateur language processing knocks: 55
100 amateur language processing knocks: 94
100 amateur language processing knocks: 54
100 amateur language processing knocks: 63
100 amateur language processing knocks: 78
100 amateur language processing knocks: 12
100 amateur language processing knocks: 14
100 amateur language processing knocks: 08
100 amateur language processing knocks: 42
100 amateur language processing knocks: 73
100 amateur language processing knocks: 75
100 amateur language processing knocks: 98
100 amateur language processing knocks: 83
100 amateur language processing knocks: 95
100 amateur language processing knocks: 32
100 amateur language processing knocks: 96
100 amateur language processing knocks: 87
100 amateur language processing knocks: 72
100 amateur language processing knocks: 79
100 amateur language processing knocks: 05
100 amateur language processing knocks: 00
100 amateur language processing knocks: 02
100 amateur language processing knocks: 37
100 amateur language processing knocks: 21
100 amateur language processing knocks: 68
100 amateur language processing knocks: 11
100 amateur language processing knocks: 90
100 amateur language processing knocks: 74
100 amateur language processing knocks: 66
100 amateur language processing knocks: 28
100 amateur language processing knocks: 64
100 amateur language processing knocks: 34
100 amateur language processing knocks: 36
100 amateur language processing knocks: 77
100 amateur language processing knocks: 01
100 amateur language processing knocks: 16
100 amateur language processing knocks: 27
100 amateur language processing knocks: 10
100 amateur language processing knocks: 03
100 amateur language processing knocks: 82
100 amateur language processing knocks: 69
100 amateur language processing knocks: 53
100 amateur language processing knocks: 18
100 amateur language processing knocks: 35
100 amateur language processing knocks: 91
100 amateur language processing knocks: 15
100 amateur language processing knocks: 38