你已經(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)。以下是它的工作原理:
大部分時(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
|
----------
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