DAY 18
2
Software Development

## Day18 - 當我們「鏈」在一起，認識zip()函數

Day4介紹python的內建函數以來，

(還沒看過題目的朋友歡迎點昨日題目傳送門)

``````def nextPrime(n):
test = n+1
while True:
if isPrime(test):
return test
test += 1
``````

# zip?拉鏈?

zip的英文是「拉鏈」或是「壓縮」，

zip可以將多個迭代器相對應位置打包成元組，

(註: 為何不是說把資料「串」起來，而說把資料「鏈」起來呢?

(嗯嗯，是女生朋友哦，不是女朋友，別亂想)

## 範例18-1: 咯咯的女生朋友

``````friends= ["芸芸", "萱萱", "琳琳", "娜娜"]
stars = ["天秤", "金牛", "雙魚","雙子"]
likes = ["餅乾", "巧克力", "草莓", "蛋糕"]
``````

``````zipped = zip(friends, stars, likes)
print(list(zipped))
``````

`[('芸芸', '天秤', '餅乾'), ('萱萱', '金牛', '巧克力'), ('琳琳', '雙魚', '草莓'), ('娜娜', '雙子', '蛋糕')]`

``````friends= ["芸芸", "萱萱", "琳琳", "娜娜"]
stars = ["天秤", "金牛", "雙魚"]
likes = ["餅乾", "巧克力", "草莓", "蛋糕"]
zipped = zip(friends, stars, likes)
print(list(zipped))
``````

`[('芸芸', '天秤', '餅乾'), ('萱萱', '金牛', '巧克力'), ('琳琳', '雙魚', '草莓')]`

## 範例18-2: 還原「鏈」起來的資料

``````people1 = ('芸芸', '天秤', '餅乾')
people2 = ('萱萱', '金牛', '巧克力')
people3 = ('琳琳', '雙魚', '草莓')
people4 = ('娜娜', '雙子', '蛋糕')
zipped = zip(people1, people2, people3, people4)
print(list(zipped))
``````

``````datas = [('芸芸', '天秤', '餅乾'), ('萱萱', '金牛', '巧克力'), ('琳琳', '雙魚', '草莓'), ('娜娜', '雙子', '蛋糕')]
zipped = zip(*datas)
print(list(zipped))
``````

(明天講解自函數定義技巧時，將更詳細講解python`*`的效果)

`zip`函數還有一些不可思議的妙用是較鮮為人知的，

## 範例18-3: 選擇題核對答案

``````answer = ['A', 'B', 'B', 'E', 'D', 'C']
wenwen = ['B', 'B', 'B', 'E', 'A', 'C']
output: 2

``````

### 普通解法

``````def check(answer, wenwen):
n = 0
for i in range(len(answer)):
if answer[i]!= wenwen[i]:
n += 1
return n
``````

### 列表生成式解法

``````def check(answer, wenwen):
return len([1 for i in range(len(answer)) if answer[i]!= wenwen[i]])
``````

### zip函數解法

zip可以把列表相對應位置起來，

``````def check(answer, wenwen):
return len([1 for x, y in zip(answer, wenwen) if x != y])
``````

## 範例18-4: 矩陣行列互換

``````原始資料datas=
[('芸芸', '天秤', '餅乾'),
('萱萱', '金牛', '巧克力'),
('琳琳', '雙魚', '草莓'),
('娜娜', '雙子', '蛋糕')]
``````

``````轉換後的資料=
[('芸芸', '萱萱', '琳琳', '娜娜'),
('天秤', '金牛', '雙魚', '雙子'),
('餅乾', '巧克力', '草莓', '蛋糕')]
``````

`A`是一個二維列表，
`list(zipped(*A))`這個操作可以將`A`的行、列元素互換。
(若你數學還不錯的話，可想成是得到轉置矩陣)

``````a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list((zip(*a))))
``````

## 範例18-5: 字典鍵(key)、值(value)互換

python的字典是可以透過「關鍵字」查詢內容的結構，

``````>>> D = {'s':"黑桃", 'h':"紅心", 'd':"方塊", 'c':"梅花"}
>>> D['s']
'黑桃'
``````

``````D = {'s':"黑桃", 'h':"紅心", 'd':"方塊", 'c':"梅花"}
print(dict(zip(D.values(), D.keys())))
``````

## 範例18-6: 矩陣向右旋轉

``````[
[1,2,3],
[4,5,6],
[7,8,9]
]
``````

``````[
[7,4,1],
[8,5,2],
[9,6,3]
]
``````

「向右旋轉90度」有蠻多方法可以解的，

``````123 (上下翻轉)  789  (行列互換)    741
456     =>     456      =>       852
789            123               963
``````

``````def rotateRight(arr):
A = arr[::-1] #利用切片上下翻轉
return list(map(list,(zip(*A)))) #行列互換，再利用map函數將zip內的元組轉列表
``````

``````arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(rotateRight(arr))
``````

(注意: 若函數內寫`return list(zip(*A))`

# 課後練習

## 習題: 矩陣向左旋轉

``````def rotateLeft(arr):
pass
``````

### 1 則留言

0
ambird
iT邦新手 5 級 ‧ 2020-09-04 16:46:35
``````def rotateLeft(arr):
A = list(map(list,(zip(*arr))))[::-1]
return A
``````