DAY 12
3
Software Development

## Day12- 第二屠龍刀一式- 列表生成式(一) 精簡的語法，利用一個列表生出另一個

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

``````def checkRepeat(plays):
return len(set(plays))<len(plays)
``````

# 第二屠龍刀- 列表生成式

，為運用一個列表生出另一個列表的語法。

``````新列表 = [something for x in 舊列表(或可迭代物件)]
``````

``````新列表 = []
for x in 舊列表(或可迭代物件):
新列表.append(something)
``````

# 範例12-1 救救我的期末分數啊

``````scores = [20,30,50,60,25,70]
``````

## 不用列表生成式怎麼解

``````new_scores = []
``````

``````adjust = 100 - max(scores)
``````

``````scores = [20,30,50,60,25,70]
new_scores = []
for s in scores:
print(new_scores)
``````

## 列表生成式怎麼解

``````scores = [20,30,50,60,25,70]
new_scores = [s+100 - max(scores) for s in scores]
``````

# 看看列表生成式的各種應用

## 生成1~10的平方數

``````>>> [x * x for x in range(1, 11)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
``````

## 將字串轉為字元陣列

``````>>> s="Hello"
>>> [c for c in s] #亦可直接寫 list(s)
['H', 'e', 'l', 'l', 'o']
``````

## 雙for迴圈生成全排列

``````>>> [m + n for m in 'abc' for n in '123']
['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']
``````

# 範例12-2: 各位數字和

6是3的倍數，所以123也是3的倍數。

``````def sumOfDigit(num):
pass
``````

## 解法一: 單純把輸入當作數字操作

``````123 % 10 = 3
123 // 10 = 12
12 % 10 = 2
12 //10 = 1
1 % 10 = 1
1 // 10 =0
``````

``````def sumOfDigit(num):
ans=0
while num>0:
ans+=num%10
num//=10
return ans
``````

## 解法二: 把輸入當作字串來處理

``````def sumOfDigit(num):
return sum([int(c) for c in str(num)])
``````

``````print(sumOfDigit(120)) #3
print(sumOfDigit(35)) #8
print(sumOfDigit(261)) #9
print(sumOfDigit(999)) #27
``````

# 範例12-3: 九九乘法表

``````1*1=1, 1*2=2, 1*3=3, …, 1*9=9,
2*1=2, 2*2=4, 2*3=6, …, 2*9=18,
…
9*1=9, 9*2=18,9*3=27, …, 9*9=81
``````

3*1 到 3*9 的結果:

``````>>> [3*i for i in range(1,10)]
[3, 6, 9, 12, 15, 18, 21, 24, 27]
``````

``````>>> [ f"3*{i}={3*i}" for i in range(1,10)]
['3*1=3',
'3*2=6',
'3*3=9',
'3*4=12',
'3*5=15',
'3*6=18',
'3*7=21',
'3*8=24',
'3*9=27']
``````

(由於螢幕空間不夠印出一整列，印出來會換行顯示)

`f"3*{i}={3*i}"`變會把大括號裡的數當作算式把值計算出來。

`[ f"4*{i}={4*i}" for i in range(1,10)]`

``````>>> [[ f"{j}*{i}={j*i}" for i in range(1,10)] for j in range(1,10)]
[['1*1=1',
'1*2=2',
'1*3=3',
'1*4=4',
'1*5=5',
'1*6=6',
'1*7=7',
'1*8=8',
'1*9=9'],
['2*1=2',
'2*2=4',
'2*3=6',
'2*4=8',
'2*5=10',
'2*6=12',
'2*7=14',
'2*8=16',
'2*9=18'],
['3*1=3',
'3*2=6',
'3*3=9',
'3*4=12',
'3*5=15',
'3*6=18',
'3*7=21',
'3*8=24',
'3*9=27'],
…(中間省略部分結果)
['8*1=8',
'8*2=16',
'8*3=24',
'8*4=32',
'8*5=40',
'8*6=48',
'8*7=56',
'8*8=64',
'8*9=72'],
['9*1=9',
'9*2=18',
'9*3=27',
'9*4=36',
'9*5=45',
'9*6=54',
'9*7=63',
'9*8=72',
'9*9=81']]
``````

``````for j in range(1,10):
print(' '.join([ f"{j}*{i}={j*i}" for i in range(1,10)]))
``````

``````1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
``````

(你可以參考官方文檔對輸出格式的說明)

``````for j in range(1,10):
print(' '.join([ f"{j}*{i}={j*i:2d}" for i in range(1,10)]))
``````

``````1*1= 1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9
2*1= 2 2*2= 4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18
3*1= 3 3*2= 6 3*3= 9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27
4*1= 4 4*2= 8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36
5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45
6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54
7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63
8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72
9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
``````

# 課後練習

## 習題: 小櫻的自訂鐵人賽

``````範例一:
input: N=1，W=[[819, 958, 508, 969, 565, 800, 703]]
output: 508 (7個整數中最小的是508)

input: N=2, W=[[740, 516, 725, 718, 861, 634, 723],
[914, 747, 580, 593, 722, 877, 595]]
output: 516 (14個整數中最小的是516)
``````

``````def findMin(N, W):
return min(W)
``````

### 1 則留言

1
ovenchang
iT邦新手 5 級 ‧ 2020-04-17 17:08:12
``````def findMin(N, W):
return min([a for ls in W for a in ls]))
``````