欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

習(xí)題 39: 列表的操作?

你已經(jīng)學(xué)過了列表。在你學(xué)習(xí)“while 循環(huán)”的時(shí)候,你對(duì)列表進(jìn)行過“追加(append)”操作,而且將列表的內(nèi)容打印了出來。另外你應(yīng)該還在加分習(xí)題里研究過 Python 文檔,看了列表支持的其他操作。這已經(jīng)是一段時(shí)間以前了,所以如果你不記得了的話,就回到本書的前面再復(fù)習(xí)一遍把。

找到了嗎?還記得嗎?很好。那時(shí)候你對(duì)一個(gè)列表執(zhí)行了 append 函數(shù)。不過,你也許還沒有真正明白發(fā)生的事情,所以我們?cè)賮砜纯次覀兛梢詫?duì)列表進(jìn)行什么樣的操作。

當(dāng)你看到像 mystuff.append('hello') 這樣的代碼時(shí),你事實(shí)上已經(jīng)在 Python 內(nèi)部激發(fā)了一個(gè)連鎖反應(yīng)。以下是它的工作原理:

  1. Python 看到你用到了 mystuff ,于是就去找到這個(gè)變量。也許它需要倒著檢查看你有沒有在哪里用 = 創(chuàng)建過這個(gè)變量,或者檢查它是不是一個(gè)函數(shù)參數(shù),或者看它是不是一個(gè)全局變量。不管哪種方式,它得先找到 mystuff 這個(gè)變量才行。
  2. 一旦它找到了 mystuff ,就輪到處理句點(diǎn) . (period) 這個(gè)操作符,而且開始查看 mystuff 內(nèi)部的一些變量了。由于 mystuff 是一個(gè)列表,Python 知道mystuff 支持一些函數(shù)。
  3. 接下來輪到了處理 append 。Python 會(huì)將 “append” 和 mystuff 支持的所有函數(shù)的名稱一一對(duì)比,如果確實(shí)其中有一個(gè)叫 append 的函數(shù),那么 Python 就會(huì)去使用這個(gè)函數(shù)。
  4. 接下來 Python 看到了括號(hào) ( (parenthesis) 并且意識(shí)到, “噢,原來這應(yīng)該是一個(gè)函數(shù)”,到了這里,它就正常會(huì)調(diào)用這個(gè)函數(shù)了,不過這里的函數(shù)還要多一個(gè)參數(shù)才行。
  5. 這個(gè)額外的參數(shù)其實(shí)是…… mystuff! 我知道,很奇怪是不是?不過這就是 Python 的工作原理,所以還是記住這一點(diǎn),就當(dāng)它是正常的好了。真正發(fā)生的事情其實(shí)是 append(mystuff, 'hello') ,不過你看到的只是 mystuff.append('hello') 。

大部分時(shí)候你不需要知道這些細(xì)節(jié),不過如果你看到一個(gè)像這樣的 Python 錯(cuò)誤信息的時(shí)候,上面的細(xì)節(jié)就對(duì)你有用了:

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Thing(object):
...     def test(hi):
...             print "hi"
...
>>> a = Thing()
>>> a.test("hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: test() takes exactly 1 argument (2 given)
>>>

就是這個(gè)嗎?嗯,這個(gè)是我在 Python 命令行下展示給你的一點(diǎn)魔法。你還沒有見過class 不過后面很快就要碰到了?,F(xiàn)在你看到 Python 說 test() takes exactly 1 argument (2 given) (test() 只可以接受1個(gè)參數(shù),實(shí)際上給了兩個(gè))。它意味著 python 把 a.test("hello") 改成了 test(a, "hello") ,而有人弄錯(cuò)了,沒有為它添加 a 這個(gè)參數(shù)。

一下子要消化這么多可能有點(diǎn)難度,不過我們將做幾個(gè)練習(xí),讓你頭腦中有一個(gè)深刻的印象。下面的練習(xí)將字符串和列表混在一起,看看你能不能在里邊找出點(diǎn)樂子來:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# create a mapping of state to abbreviation
states = {
    'Oregon': 'OR',
    'Florida': 'FL',
    'California': 'CA',
    'New York': 'NY',
    'Michigan': 'MI'
}

# create a basic set of states and some cities in them
cities = {
    'CA': 'San Francisco',
    'MI': 'Detroit',
    'FL': 'Jacksonville'
}

# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Portland'

# print out some cities
print '-' * 10
print "NY State has: ", cities['NY']
print "OR State has: ", cities['OR']

# print some states
print '-' * 10
print "Michigan's abbreviation is: ", states['Michigan']
print "Florida's abbreviation is: ", states['Florida']

# do it by using the state then cities dict
print '-' * 10
print "Michigan has: ", cities[states['Michigan']]
print "Florida has: ", cities[states['Florida']]

# print every state abbreviation
print '-' * 10
for state, abbrev in states.items():
    print "%s is abbreviated %s" % (state, abbrev)

# print every city in state
print '-' * 10
for abbrev, city in cities.items():
    print "%s has the city %s" % (abbrev, city)

# now do both at the same time
print '-' * 10
for state, abbrev in states.items():
    print "%s state is abbreviated %s and has city %s" % (
        state, abbrev, cities[abbrev])

print '-' * 10
# safely get a abbreviation by state that might not be there
state = states.get('Texas', None)

if not state:
    print "Sorry, no Texas."

# get a city with a default value
city = cities.get('TX', 'Does Not Exist')
print "The city for the state 'TX' is: %s" % city

你應(yīng)該看到的結(jié)果?

----------
NY State has:  New York
OR State has:  Portland
----------
Michigan's abbreviation is:  MI
Florida's abbreviation is:  FL
----------
Michigan has:  Detroit
Florida has:  Jacksonville
----------
California is abbreviated CA
Michigan is abbreviated MI
New York is abbreviated NY
Florida is abbreviated FL
Oregon is abbreviated OR
----------
FL has the city Jacksonville
CA has the city San Francisco
MI has the city Detroit
OR has the city Portland
NY has the city New York
----------
California state is abbreviated CA and has city San Francisco
Michigan state is abbreviated MI and has city Detroit
New York state is abbreviated NY and has city New York
Florida state is abbreviated FL and has city Jacksonville
Oregon state is abbreviated OR and has city Portland
----------
Sorry, no Texas.
The city for the state 'TX' is: Does Not Exist

加分習(xí)題?

  1. 將每一個(gè)被調(diào)用的函數(shù)以上述的方式翻譯成 Python 實(shí)際執(zhí)行的動(dòng)作。例如: ' '.join(things) 其實(shí)是 join(' ', things) 。
  2. 將這兩種方式翻譯為自然語言。例如, ' '.join(things) 可以翻譯成“用 ‘ ‘ 連接(join) things”,而 join(' ', things) 的意思是“為 ‘ ‘ 和 things 調(diào)用 join 函數(shù)”。這其實(shí)是同一件事情。
  3. 上網(wǎng)閱讀一些關(guān)于“面向?qū)ο缶幊?Object Oriented Programming)”的資料。暈了吧?嗯,我以前也是。別擔(dān)心。你將從這本書學(xué)到足夠用的關(guān)于面向?qū)ο缶幊痰幕A(chǔ)知識(shí),而以后你還可以慢慢學(xué)到更多。
  4. 查一下 Python中的 “class” 是什么東西。不要閱讀關(guān)于其他語言的 “class” 的用法,這會(huì)讓你更糊涂。
  5. dir(something)something 的 class 有什么關(guān)系?
  6. 如果你不知道我講的是些什么東西,別擔(dān)心。程序員為了顯得自己聰明,于是就發(fā)明了 Object Oriented Programming,簡(jiǎn)稱為 OOP,然后他們就開始濫用這個(gè)東西了。如果你覺得這東西太難,你可以開始學(xué)一下 “函數(shù)編程(functional programming)”。

Project Versions

Table Of Contents

Previous topic

習(xí)題 38: 閱讀代碼

Next topic

習(xí)題 40: 字典, 可愛的字典

This Page