zip nesnesi nedir?

Python ile Kodlama Mülakatı Soruları Pratiği

Kirill Smirnov

Data Science Consultant, Altran

Tanım

zip - birden çok yinelenebilir nesneyi tek bir yinelenebilir nesnede birleştiren nesne.

Yinelenebilirlerin kümesi

$\text{e}_\text{i}$ - Bir Yinelenebilir'den bir öğe

Python ile Kodlama Mülakatı Soruları Pratiği

Tanım

zip - birden çok yinelenebilir nesneyi tek bir yinelenebilir nesnede birleştiren nesne.

Birkaç Yinelenebiliri bir zip nesnesinde birleştirme

$\text{e}_\text{i}$ - Bir Yinelenebilir'den bir öğe

Python ile Kodlama Mülakatı Soruları Pratiği

Tanım

zip - birden çok yinelenebilir nesneyi tek bir yinelenebilir nesnede birleştiren nesne.

zip nesnesinde yineleme

$\text{e}_\text{i}$ - Bir Yinelenebilir'den bir öğe

Python ile Kodlama Mülakatı Soruları Pratiği

Örnek

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 ile Kodlama Mülakatı Soruları Pratiği

Bir zip nesnesinde gezinme

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
('N', 'Bebop', 'Leonardo')
('T', 'Rocksteady', 'Donatello')
Python ile Kodlama Mülakatı Soruları Pratiği

Demet listesini döndürme

result = zip(title, villains, turtles)
tuples = list(result)
print(tuples)
[
    ('T', 'Shredder', 'Raphael'), ('M', 'Krang', 'Michelangelo'),
    ('N', 'Bebop', 'Leonardo'), ('T', 'Rocksteady', 'Donatello')
]
Python ile Kodlama Mülakatı Soruları Pratiği

Yineleyici olarak zip nesnesi

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 ile Kodlama Mülakatı Soruları Pratiği

zip nesnesi tüketilirdir

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('M', 'Krang', 'Michelangelo')
('N', 'Bebop', 'Leonardo')
('T', 'Rocksteady', 'Donatello')
Python ile Kodlama Mülakatı Soruları Pratiği

zip nesnesi tüketilirdir

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 ile Kodlama Mülakatı Soruları Pratiği

'zip' nesnesi tüketilirdir

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 ile Kodlama Mülakatı Soruları Pratiği

Eşit olmayan yinelenebilir boyutları

title = 'TMNT'
villains = ['Shredder', 'Krang', 'Bebop', 'Rocksteady']
turtles = {
    'Raphael': 'Sai', 'Michelangelo': 'Nunchaku',
    'Leonardo': 'Twin katana', 'Donatello': 'Bo'
}
Python ile Kodlama Mülakatı Soruları Pratiği

Eşit olmayan yinelenebilir boyutları

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 ile Kodlama Mülakatı Soruları Pratiği

'zip' nesnesinde gezinme

result = zip(title, villains, turtles)
for item in result:
    print(item)
('T', 'Shredder', 'Raphael')
('e', 'Krang', 'Michelangelo')
('e', 'Bebop', 'Leonardo')
('n', 'Rocksteady', 'Donatello')
Python ile Kodlama Mülakatı Soruları Pratiği

Ters işlem

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 ile Kodlama Mülakatı Soruları Pratiği

Eşit olmayan demet boyutları

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 ile Kodlama Mülakatı Soruları Pratiği

Sözlük ile ilişkisi

Bir zip nesnesi sözlük oluşturmak için kullanılabilir

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 ile Kodlama Mülakatı Soruları Pratiği

DataFrame oluşturma

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 ile Kodlama Mülakatı Soruları Pratiği

DataFrame oluşturma

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 ile Kodlama Mülakatı Soruları Pratiği

DataFrame oluşturma

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 ile Kodlama Mülakatı Soruları Pratiği

DataFrame oluşturma

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 ile Kodlama Mülakatı Soruları Pratiği

Ayo berlatih!

Python ile Kodlama Mülakatı Soruları Pratiği

Preparing Video For Download...