Zip object là gì?

Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Kirill Smirnov

Data Science Consultant, Altran

Định nghĩa

zip - đối tượng kết hợp nhiều iterable thành một iterable.

Tập hợp các Iterable

$\text{e}_\text{i}$ - một phần tử của Iterable

Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Định nghĩa

zip - đối tượng kết hợp nhiều iterable thành một iterable.

Kết hợp nhiều Iterable thành một đối tượng zip

$\text{e}_\text{i}$ - một phần tử của Iterable

Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Định nghĩa

zip - đối tượng kết hợp nhiều iterable thành một iterable.

Duyệt qua đối tượng zip

$\text{e}_\text{i}$ - một phần tử của Iterable

Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Ví dụ

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>
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Duyệt qua đối tượng zip

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
('N', 'Bebop', 'Leonardo')
('T', 'Rocksteady', 'Donatello')
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Trả về danh sách các tuple

result = zip(title, villains, turtles)
tuples = list(result)
print(tuples)
[
    ('T', 'Shredder', 'Raphael'), ('M', 'Krang', 'Michelangelo'),
    ('N', 'Bebop', 'Leonardo'), ('T', 'Rocksteady', 'Donatello')
]
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

zip là Iterator

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
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

zip dùng một lần (tiêu hao)

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
('N', 'Bebop', 'Leonardo')
('T', 'Rocksteady', 'Donatello')
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

zip dùng một lần (tiêu hao)

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')
]
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Đối tượng 'zip' dùng một lần

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)
[]
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Kích thước Iterable không bằng nhau

title = 'TMNT'
villains = ['Shredder', 'Krang', 'Bebop', 'Rocksteady']
turtles = {
    'Raphael': 'Sai', 'Michelangelo': 'Nunchaku',
    'Leonardo': 'Twin katana', 'Donatello': 'Bo'
}
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Kích thước Iterable không bằng nhau

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)
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Duyệt qua đối tượng 'zip'

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('e', 'Krang', 'Michelangelo')
('e', 'Bebop', 'Leonardo')
('n', 'Rocksteady', 'Donatello')
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Phép đảo ngược

turtle_masks = [
    ('Raphael', 'red'), ('Michelangelo', 'orange'),
    ('Leonardo', 'blue'), ('Donatello', 'purple')
]
result = zip(*turtle_masks)
print(result)
[
    ('Raphael', 'Michelangelo', 'Leonardo', 'Donatello'),
    ('red', 'orange', 'blue', 'purple')
]
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Kích thước tuple không bằng nhau

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')
]
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Quan hệ với dictionary

Một đối tượng zip có thể dùng để tạo 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]
}
Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Tạo 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()

Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Tạo 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()

Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Tạo 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()

Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Tạo 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()

Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Ayo berlatih!

Luyện tập câu hỏi phỏng vấn lập trình bằng Python

Preparing Video For Download...