1. 程式人生 > >第四章-操作列表

第四章-操作列表

系列 first 變量 numbers 句子 gre ike 自助餐 any

4-1 比薩 : 想出至少是三種你喜歡的比薩,將其名稱存儲在一個列表中,再使用for循環將每種比薩的名稱都打印出來。

修改這個for循環,使其打印包含比薩名稱的矩陣,而不僅僅是比薩的名稱。對於每種比薩,都顯示一行輸出,如“I like pepperoni pizza”。

在程序末尾添加一行代碼,它不在for循環中,指出你有多喜歡比薩。輸出應包含針對每種比薩的消息,還有一個總結性矩陣,如“I really love pizza!”

1 pizzas=[a,b,c]
2 for pizz in pizzas:
3     print(pizz)
4 for pizza in pizzas:
5 print("I like "+pizza+" pizza.") 6 print(I really like pizza.)

輸出:

1 a
2 b
3 c
4 I like a pizza.
5 I like b pizza.
6 I like c pizza.
7 I really like pizza.

4-2 動物 :想出至少三種有共同特征的動物,將這些動物的名稱存儲在一個列表中,再使用for 循環將每種動物的名稱都打印出來。

修改這個程序,使其針對每種動物都打印一個句子,如“A dog would make a great pet”。

在程序末尾添加一行代碼,指出這些動物的共同之處,如打印諸如“Any of these animals would make a great pet!”這樣的句子。

1 animals=[cat,dog,pig]
2 for anima in animals:
3     print(anima)
4 for animal in animals:
5     print("A "+animal+" would make a great pet.")
6 print("Any of these animals would make a great pet.")

輸出:

1 cat
2 dog
3 pig
4 A cat would make a great pet.
5 A dog would make a great pet.
6 A pig would make a great pet.
7 Any of these animals would make a great pet.

4-3 數到20 :使用一個for 循環打印數字1~20(含)。

1 for value in range(1,21):#range()生成一系列數字
2     print(value)

輸出:

 1 1
 2 2
 3 3
 4 4
 5 5
 6 6
 7 7
 8 8
 9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
20 20

4-4 一百萬 :創建一個列表,其中包含數字1~1 000 000,再使用一個for 循環將這些數字打印出來(如果輸出的時間太長,按Ctrl + C停止輸出,或關閉輸出窗口)。

1 numbers=list(range(1,10))#list()將range()的結果直接轉換為列表。
2 print(numbers)

輸出:

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

4-5 計算1~1 000 000的總和 :創建一個列表,其中包含數字1~1 000 000,再使用min() 和max() 核實該列表確實是從1開始,到1 000 000結束的。另外,對這個列表 調用函數sum() ,看看Python將一百萬個數字相加需要多長時間。

1 numbers=list(range(1,1000001))
2 print(min(numbers))
3 print(max(numbers))
4 print(sum(numbers))

輸出:

1 1
2 1000000
3 500000500000

4-6 奇數 :通過給函數range() 指定第三個參數來創建一個列表,其中包含1~20的奇數;再使用一個for 循環將這些數字都打印出來。

1 numbers=list(range(1,21,2))
2 for number in numbers:
3     print(number)

輸出:

 1 1
 2 3
 3 5
 4 7
 5 9
 6 11
 7 13
 8 15
 9 17
10 19

4-7 3的倍數 :創建一個列表,其中包含3~30內能被3整除的數字;再使用一個for 循環將這個列表中的數字都打印出來。

1 numbers=list(range(3,31,3))
2 for number in numbers:
3     print(number)

輸出:

 1 3
 2 6
 3 9
 4 12
 5 15
 6 18
 7 21
 8 24
 9 27
10 30

4-8 立方 :將同一個數字乘三次稱為立方。例如,在Python中,2的立方用2**3 表示。請創建一個列表,其中包含前10個整數(即1~10)的立方,再使用一個for 循 環將這些立方數都打印出來。

1 numbers=list(range(1,11))
2 for number in numbers:
3     print(number**3)

輸出:

 1 1
 2 8
 3 27
 4 64
 5 125
 6 216
 7 343
 8 512
 9 729
10 1000

4-9 立方解析 :使用列表解析生成一個列表,其中包含前10個整數的立方

1 lifang=[value**3 for value in range(1,11)]#for末尾沒有冒號
2 print(lifang)

輸出:

1 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

4-10 切片 :選擇你在本章編寫的一個程序,在末尾添加幾行代碼,以完成如下任務。

打印消息“The first three items in the list are:”,再使用切片來打印列表的前三個元素。

打印消息“Three items from the middle of the list are:”,再使用切片來打印列表中間的三個元素。

打印消息“The last three items in the list are:”,再使用切片來打印列表末尾的三個元素。

1 lifang=[value**3 for value in range(1,11)]#for末尾沒有冒號
2 print(lifang)
3 print("The first three itemss in the list are:")
4 print(lifang[0:3])
5 print("Three items from the middle of the list are:")
6 print(lifang[4:7])
7 print(The last three items in the list are:)
8 print(lifang[-3:])

輸出:

1 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
2 The first three itemss in the list are:
3 [1, 8, 27]
4 Three items from the middle of the list are:
5 [125, 216, 343]
6 The last three items in the list are:
7 [512, 729, 1000]

4-11 你的比薩和我的比薩 :在你為完成練習4-1而編寫的程序中,創建比薩列表的副本,並將其存儲到變量friend_pizzas 中,再完成如下任務。
在原來的比薩列表中添加一種比薩。

在列表friend_pizzas 中添加另一種比薩。

核實你有兩個不同的列表。為此,打印消息“My favorite pizzas are:”,再使用一個for 循環來打印第一個列表;打印消息“My friend‘s favorite pizzas are:”,再使用一 個for 循環 來打印第二個列表。核實新增的比薩被添加到了正確的列表中。

 1 pizzas=[a,b,c]
 2 friends_pizza=pizzas[:]
 3 pizzas.append(d)
 4 friends_pizza.append(e)
 5 print(My favorite pizzas are:)
 6 for pizza in pizzas:
 7     print(pizza)
 8 print("My friend‘s favorite pizzas are: ")
 9 for friend_pizza in friends_pizza:
10     print(friend_pizza)

輸出:

 1 My favorite pizzas are:
 2 a
 3 b
 4 c
 5 d
 6 My friends favorite pizzas are: 
 7 a
 8 b
 9 c
10 e

4-12 使用多個循環 :在本節中,為節省篇幅,程序foods.py的每個版本都沒有使用for 循環來打印列表。請選擇一個版本的foods.py,在其中編寫兩個for 循環,將各 個食品列表都打印出來。

1 my_foods=[pizza,falafel,carrot cake]
2 friend_foods=my_foods[:]
3 print("My favorite foods are;")
4 for my_food in my_foods:
5     print(my_food)
6 print("My friend‘s favorite foods are:")
7 for friend_food in friend_foods:
8     print(friend_food)

輸出:

1 My favorite foods are;
2 pizza
3 falafel
4 carrot cake
5 My friends favorite foods are:
6 pizza
7 falafel
8 carrot cake

4-13 自助餐 自助餐 :有一家自助式餐館,只提供五種簡單的食品。請想出五種簡單的食品,並將其存儲在一個元組中。

使用一個for 循環將該餐館提供的五種食品都打印出來。

嘗試修改其中的一個元素,核實Python確實會拒絕你這樣做。

餐館調整了菜單,替換了它提供的其中兩種食品。請編寫一個這樣的代碼塊:給元組變量賦值,並使用一個for 循環將新元組的每個元素都打印出來。

 1 #4-13
 2 foods=(pizza,falafel,cake,cannoli,ice_cream)#要有引號
 3 for food in foods:
 4     print(food)
 5 #foods[0]=apple
 6 foods=(apple,banana,carrot_cake,cannoli,ice_cream)
 7 for food1 in foods:
 8     print(food1)
 9 num=(250,100)#數字沒有引號
10 print(num[0])

輸出:

 1 pizza
 2 falafel
 3 cake
 4 cannoli
 5 ice_cream
 6 apple
 7 banana
 8 carrot_cake
 9 cannoli
10 ice_cream
11 250

第四章-操作列表