練習(xí) 32 循環(huán)和列表
你現(xiàn)在可以做一些更有意思的程序了。如果你一直跟著我們的節(jié)奏,你應(yīng)該可以把所有學(xué)過(guò)的東西用 if 語(yǔ)句和布爾表達(dá)式結(jié)合起來(lái),讓你的程序做一些好玩的事情。
不過(guò),程序仍然需要快速做一些重復(fù)的事情。我們要在這個(gè)練習(xí)中用一個(gè) for-loop 來(lái)創(chuàng)建和打印各種列表。你在做練習(xí)的時(shí)候,得想想它們是什么。我不會(huì)立馬告訴你,你得自己去想。
在你能夠用一個(gè) for-loop 之前,你需要一種方法來(lái)把這些循環(huán)的結(jié)果儲(chǔ)存在某處。最好的辦法就是用列表。列表顧名思義就是一個(gè)按順序從頭到尾組成的某種東西的容器。它并不復(fù)雜:你只需要學(xué)習(xí)一個(gè)新的語(yǔ)法。首先,你可以這樣創(chuàng)建列表:
hairs = ['brown', 'blond', 'red']
eyes = ['brown', 'blue', 'green'] weights = [1, 2, 3, 4]
以左方括號(hào)( [ )開(kāi)始打開(kāi)列表,然后把你想要的條目用逗號(hào)隔開(kāi)放進(jìn)去,有點(diǎn)類似于函數(shù)的參數(shù)。最后,用右方括號(hào)( ] )來(lái)表明列表的結(jié)束。Python 會(huì)選取這個(gè)列表以及它的所有內(nèi)容并把它們分配到變量里。
| 警告! |
|---|
| 這塊內(nèi)容對(duì)于不會(huì)編程的人來(lái)說(shuō)有點(diǎn)難理解,因?yàn)槟愕拇竽X一直以來(lái)都被訓(xùn)練成平面的了。還記得上個(gè)練習(xí)中你把 if 語(yǔ)句放在 if 語(yǔ)句中嗎?這可能讓你傷腦筋了,因?yàn)榇蠖鄶?shù)人不理解如何在一個(gè)東西里面嵌套一個(gè)東西。在編程中,嵌套結(jié)構(gòu)無(wú)處不在。你會(huì)看到一個(gè)函數(shù)調(diào)用其他包含 if 語(yǔ)句的函數(shù),這個(gè) if 語(yǔ)句中又有一個(gè)包含列表的列表。如果你看到一個(gè)類似的結(jié)構(gòu)無(wú)法理解,拿出一根筆和一張紙,然后手動(dòng)把它拆解開(kāi),直到你完全理解為止。 |
我們現(xiàn)在要用 for-loops 來(lái)創(chuàng)建一些列表,然后把它們打印出來(lái)。
ex32.py
1 the_count = [1, 2, 3, 4, 5]
2 fruits = ['apples', 'oranges', 'pears', 'apricots']
3 change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
4
5 # this first kind of for-loop goes through a list
6 for number in the_count:
7 print(f"This is count {number}")
8
9 # same as above
10 for fruit in fruits:
11 print(f"A fruit of type: {fruit}")
12
13 # also we can go through mixed lists too
14 # notice we have to use {} since we don't know what's in it
15 for i in change:
16 print(f"I got {i}")
17
18 # we can also build lists, first start with an empty one
19 elements = []
20
21 # then use the range function to do 0 to 5 counts
22 for i in range(0, 6):
23 print(f"Adding {i} to the list.")
24 # append is a function that lists understand
25 elements.append(i)
26
27 # now we can print them out too
28 for i in elements:
29 print(f"Element was: {i}")
你會(huì)看到
練習(xí) 32 會(huì)話
$ python3.6 ex32.py
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: apricots
I got 1
I got pennies
I got 2
I got dimes
I got 3
I got quarters
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5
附加練習(xí)
- 看看你是如何使用 range 的。查閱上面的 range 函數(shù)并理解掌握。
- 你能在第 22 行不使用 for-loop,而是直接把 range(0, 6) 賦給 elements 嗎?
- 找到 Python 文檔關(guān)于列表的部分,然后讀一讀??纯闯?append,你還能對(duì)列表做哪些操作?
常見(jiàn)問(wèn)題
如何創(chuàng)建一個(gè)二維列表?可以用這種列表中的列表:[[1,2,3],[4,5,6]]
列表(lists)和數(shù)組(arrays)難道不是一個(gè)東西嗎?這取決于語(yǔ)言以及實(shí)現(xiàn)方法。在傳統(tǒng)術(shù)語(yǔ)中,列表和數(shù)組的實(shí)現(xiàn)方式不同。在 Ruby 中都叫做 arrays,在 python 中都叫做 lists。所以我們就把這些叫做列表吧。
為什么 for-loop 可以用一個(gè)沒(méi)有被定義的變量?變量在 for-loop 開(kāi)始的時(shí)候就被定義了,它被初始化到了每一次 loop 迭代時(shí)的當(dāng)前元素中。
為什么 range(1, 3) 中的 i 只循環(huán)了兩次而不是三次? range() 函數(shù)只處理從第一個(gè)到最后一個(gè)數(shù),但不包括最后一個(gè)數(shù),所以它在 2 就結(jié)束了。這是這類循環(huán)的通用做法。
element.append() 的作用是什么?它只是把東西追加到列表的末尾。打開(kāi) Python shell 然后創(chuàng)建一個(gè)新列表。任何時(shí)候當(dāng)你遇到類似的用法,試著多玩幾次,去體會(huì)它們的作用。