zip オブジェクトとは?

Pythonでコーディング面接問題を練習する

Kirill Smirnov

Data Science Consultant, Altran

定義

zip — 複数のイテラブルを1つのイテラブルにまとめるオブジェクト。

イテラブルの集合

$\text{e}_\text{i}$ — イテラブルの要素

Pythonでコーディング面接問題を練習する

定義

zip — 複数のイテラブルを1つのイテラブルにまとめるオブジェクト。

複数のイテラブルを zip オブジェクトに結合

$\text{e}_\text{i}$ — イテラブルの要素

Pythonでコーディング面接問題を練習する

定義

zip — 複数のイテラブルを1つのイテラブルにまとめるオブジェクト。

zip オブジェクトの走査

$\text{e}_\text{i}$ — イテラブルの要素

Pythonでコーディング面接問題を練習する

例

title = 'TMNT'
villains = ['Shredder', 'Krang', 'Bebop', 'Rocksteady']
turtles = {
    'Raphael': 'Sai', 'Michelangelo': 'Nunchaku',
    'Leonardo': 'Twin katana', 'Donatello': 'Bo'
}
result = zip(title, villains, turtles)
print(result)
<zip object at 0x7f37bab6e608>
Pythonでコーディング面接問題を練習する

zip オブジェクトの走査

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
('N', 'Bebop', 'Leonardo')
('T', 'Rocksteady', 'Donatello')
Pythonでコーディング面接問題を練習する

タプルのリストを返す

result = zip(title, villains, turtles)
tuples = list(result)
print(tuples)
[
    ('T', 'Shredder', 'Raphael'), ('M', 'Krang', 'Michelangelo'),
    ('N', 'Bebop', 'Leonardo'), ('T', 'Rocksteady', 'Donatello')
]
Pythonでコーディング面接問題を練習する

イテレータとしての zip オブジェクト

result = zip(title, villains, turtles)
next(result)
('T', 'Shredder', 'Raphael')
next(result)
('M', 'Krang', 'Michelangelo')
next(result)
('N', 'Bebop', 'Leonardo')
next(result)
('T', 'Rocksteady', 'Donatello')
next(result)
StopIteration
Pythonでコーディング面接問題を練習する

zip オブジェクトは使い切り

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
('N', 'Bebop', 'Leonardo')
('T', 'Rocksteady', 'Donatello')
Pythonでコーディング面接問題を練習する

zip オブジェクトは使い切り

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
...
for item in result:
    print(item)
# nothing
result = zip(title, villains, turtles)
tuples = list(result)
print(tuples)
[
    ('T', 'Shredder', 'Raphael'),
    ('M', 'Krang', 'Michelangelo'),
    ('N', 'Bebop', 'Leonardo'),
    ('T', 'Rocksteady', 'Donatello')
]
Pythonでコーディング面接問題を練習する

zip オブジェクトは使い切り

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
...
for item in result:
    print(item)
# nothing
result = zip(title, villains, turtles)
tuples = list(result)
print(tuples)
[
    ('T', 'Shredder', 'Raphael'),
    ...
tuples = list(result)
print(tuples)
[]
Pythonでコーディング面接問題を練習する

イテラブルの長さが不一致

title = 'TMNT'
villains = ['Shredder', 'Krang', 'Bebop', 'Rocksteady']
turtles = {
    'Raphael': 'Sai', 'Michelangelo': 'Nunchaku',
    'Leonardo': 'Twin katana', 'Donatello': 'Bo'
}
Pythonでコーディング面接問題を練習する

イテラブルの長さが不一致

title = 'Teenage Mutant Ninja Turtles'
villains = ['Shredder', 'Krang', 'Bebop', 'Rocksteady']
turtles = {
    'Raphael': 'Sai', 'Michelangelo': 'Nunchaku',
    'Leonardo': 'Twin katana', 'Donatello': 'Bo'
}
result = zip(title, villains, turtles)
Pythonでコーディング面接問題を練習する

zip オブジェクトの走査

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('e', 'Krang', 'Michelangelo')
('e', 'Bebop', 'Leonardo')
('n', 'Rocksteady', 'Donatello')
Pythonでコーディング面接問題を練習する

逆操作

turtle_masks = [
    ('Raphael', 'red'), ('Michelangelo', 'orange'),
    ('Leonardo', 'blue'), ('Donatello', 'purple')
]
result = zip(*turtle_masks)
print(result)
[
    ('Raphael', 'Michelangelo', 'Leonardo', 'Donatello'),
    ('red', 'orange', 'blue', 'purple')
]
Pythonでコーディング面接問題を練習する

タプル長の不一致

turtle_masks = [
    ('Raphael', 'red'), ('Michelangelo', 'orange'),
    ('Leonardo', 'blue', 'cyan'), ('Donatello', 'purple', 'magenta')
]
result = zip(*turtle_masks)
print(result)
[
    ('Raphael', 'Michelangelo', 'Leonardo', 'Donatello'),
    ('red', 'orange', 'blue', 'purple')
]
Pythonでコーディング面接問題を練習する

辞書との関係

A zip object can be used to create a dictionary

keys = ['movie', 'year', 'director']
values = [
    ['Forest Gump', 'Goodfellas', 'Se7en'],
    [1994, 1990, 1995],
    ['R.Zemeckis', 'M.Scorsese', 'D.Fincher']
]
movies = dict(zip(keys, values))
print(movies)
{
    'director': [
        'R.Zemeckis',
        'M.Scorsese',
        'D.Fincher'
    ], 
    'movie': [
        'Forest Gump',
        'Goodfellas',
        'Se7en'
    ],
    'year': [1994, 1990, 1995]
}
Pythonでコーディング面接問題を練習する

DataFrame の作成

import pandas as pd

df_movies = pd.DataFrame(movies)
print(df_movies)
          director        movie  year
0  Robert Zemeckis  Forest Gump  1994
1  Martin Scorsese   Goodfellas  1990
2    David Fincher        Se7en  1995

list()

Pythonでコーディング面接問題を練習する

DataFrame の作成

import pandas as pd

df_movies = pd.DataFrame(movies)
print(df_movies)
          director        movie  year
0  Robert Zemeckis  Forest Gump  1994
1  Martin Scorsese   Goodfellas  1990
2    David Fincher        Se7en  1995

list() $\rightarrow$ zip()

Pythonでコーディング面接問題を練習する

DataFrame の作成

import pandas as pd

df_movies = pd.DataFrame(movies)
print(df_movies)
          director        movie  year
0  Robert Zemeckis  Forest Gump  1994
1  Martin Scorsese   Goodfellas  1990
2    David Fincher        Se7en  1995

list() $\rightarrow$ zip() $\rightarrow$ dict()

Pythonでコーディング面接問題を練習する

DataFrame の作成

import pandas as pd

df_movies = pd.DataFrame(movies)
print(df_movies)
          director        movie  year
0  Robert Zemeckis  Forest Gump  1994
1  Martin Scorsese   Goodfellas  1990
2    David Fincher        Se7en  1995

list() $\rightarrow$ zip() $\rightarrow$ dict() $\rightarrow$ DataFrame()

Pythonでコーディング面接問題を練習する

Let's practice!

Pythonでコーディング面接問題を練習する

Preparing Video For Download...