# Python 中的 zip() 函式

Vaibhav Vaibhav 2022年5月18日

Python 使這項任務變得容易得多。我們可以使用 Python 提供的內建實用程式，更準確地說是一個函式，而不是手動編寫用於迭代不同大小陣列的邏輯。這個函式是 `zip()` 函式。

## Python 中的 `zip()` 函式

`zip()` 函式接受可迭代物件（例如列表、字串和元組）作為引數並返回單個可迭代物件。

### 沒有可迭代物件

``````result = zip()

for x in result:
print(x)
``````

### 相同長度的可迭代物件

``````class Number:
def __init__(self, number):
self.number = number

def square(self):
return number ** 2

def __repr__(self):
return f"Number({self.number})"

a = (11, 22, 33, 44, 55)
b = [1.1, 2.2, 3.3, 4.4, 5.5]
c = [Number(1), Number(23), Number(44.44), Number(0), Number(-9)]
d = "Hello"
result = zip(a, b, c, d)

for x in result:
print(x)
``````

``````(11, 1.1, Number(1), 'H')
(22, 2.2, Number(23), 'e')
(33, 3.3, Number(44.44), 'l')
(44, 4.4, Number(0), 'l')
(55, 5.5, Number(-9), 'o')
``````

``````class Number:
def __init__(self, number):
self.number = number

def square(self):
return number ** 2

def __repr__(self):
return f"Number({self.number})"

a = (11, 22, 33, 44, 55)
b = [1.1, 2.2, 3.3, 4.4, 5.5]
c = [Number(1), Number(23), Number(44.44), Number(0), Number(-9)]
d = "Hello"
result = zip(a, b, c, d)

for p, q, r, s in result:
print("A:", p)
print("B:", q)
print("C:", r)
print("D:", s)
``````

``````A: 11
B: 1.1
C: Number(1)
D: H
A: 22
B: 2.2
C: Number(23)
D: e
A: 33
B: 3.3
C: Number(44.44)
D: l
A: 44
B: 4.4
C: Number(0)
D: l
A: 55
B: 5.5
C: Number(-9)
D: o
``````

``````class Number:
def __init__(self, number):
self.number = number

def square(self):
return number ** 2

def __repr__(self):
return f"Number({self.number})"

a = (11, 22, 33, 44, 55)
b = [1.1, 2.2, 3.3, 4.4, 5.5]
c = [Number(1), Number(23), Number(44.44), Number(0), Number(-9)]
d = "Hello"
result = zip(a, b, c, d)

while True:
try:
p, q, r, s = next(result)
print("A:", p)
print("B:", q)
print("C:", r)
print("D:", s)
except StopIteration:
break
``````

``````A: 11
B: 1.1
C: Number(1)
D: H
A: 22
B: 2.2
C: Number(23)
D: e
A: 33
B: 3.3
C: Number(44.44)
D: l
A: 44
B: 4.4
C: Number(0)
D: l
A: 55
B: 5.5
C: Number(-9)
D: o
``````

### 不同長度的可迭代物件

``````class Number:
def __init__(self, number):
self.number = number

def square(self):
return number ** 2

def __repr__(self):
return f"Number({self.number})"

a = (11, 22, 33)
b = [1.1, 2.2, 3.3, 4.4]
c = [Number(1), Number(23), Number(44.44), Number(0), Number(-9)]
d = "HelloWorld"
result = zip(a, b, c, d)

for p, q, r, s in result:
print("A:", p)
print("B:", q)
print("C:", r)
print("D:", s)
``````

``````A: 11
B: 1.1
C: Number(1)
D: H
A: 22
B: 2.2
C: Number(23)
D: e
A: 33
B: 3.3
C: Number(44.44)
D: l
``````

### 建立字典

``````import json

a = ["W", "O", "R", "L", "D"]
b = [1.1, True, "Hello", None, 5]
result = zip(a, b)
mapping = {x: y for x, y in result}
print(json.dumps(mapping, indent=4))
``````

``````{
"W": 1.1,
"O": true,
"R": "Hello",
"L": null,
"D": 5
}
``````

### 將 `zip()` 函式與 `enumerate()` 函式一起使用

`enumerate()` 函式用於在迭代可迭代物件時同時獲取索引和值。由於 `zip()` 函式返回一個迭代器，我們可以將這兩個函式組合在一起並訪問索引和值。請參閱以下 Python 程式碼。

``````class Number:
def __init__(self, number):
self.number = number

def square(self):
return number ** 2

def __repr__(self):
return f"Number({self.number})"

a = (11, 22, 33)
b = [1.1, 2.2, 3.3, 4.4]
c = [Number(1), Number(23), Number(44.44), Number(0), Number(-9)]
d = "HelloWorld"
result = zip(a, b, c, d)

for i, (p, q, r, s) in enumerate(result):
print(f"A{i + 1}:", p)
print(f"B{i + 1}:", q)
print(f"C{i + 1}:", r)
print(f"D{i + 1}:", s)
``````

``````A1: 11
B1: 1.1
C1: Number(1)
D1: H
A2: 22
B2: 2.2
C2: Number(23)
D2: e
A3: 33
B3: 3.3
C3: Number(44.44)
D3: l
``````

`enumerate()` 函式返回的值採用以下格式。

``````(0, (11, 1.1, Number(1), "H"))
(1, (22, 2.2, Number(23), "e"))
(2, (33, 3.3, Number(44.44), "l"))
``````

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.