1. 程式人生 > >笨方法學Python 習題 38: 列表的操作

笨方法學Python 習題 38: 列表的操作

#!usr/bin/python
# -*-coding:utf-8-*-

states = {
	"Oregon":"OR",
	"Floriad":"FL",
	"California":"CA",
	"New York":"NY",
	"Michigan":"MI"
}

cities = {
	"CA":"San Francisco",
	"MI":"Detroit",
	"FL":"Jacksonville"
}

cities["NY"] = "New York"
cities["OR"] = "Portland"

print("_"*10)
print("NY State has:",cities["NY"])
print("OR State has:",cities["OR"])
#NY State has:  New York
#OR State has:  Portland

print("-"*10)
print("Michigan's abbreviation is: ", states['Michigan'])
print("Florida's abbreviation is: ", states['Floriad'])
#Michigan's abbreviation is:  MI
#Florida's abbreviation is:  FL

print('-' * 10)
print("Michigan has: ", cities[states['Michigan']])
print("Floriad has: ", cities[states['Floriad']])
#Michigan has:  Detroit
#Florida has:  Jacksonville

print("-"*10)
for state , abbrev in states.items():
	print("%s is abbreviated %s"%(state,abbrev))
# items() 函式以列表返回可遍歷的(鍵, 值) 元組陣列。
#California is abbreviated CA
#Michigan is abbreviated MI
#New York is abbreviated NY
#Florida is abbreviated FL
#Oregon is abbreviated OR

print("-"*10)
for abbrev , city in cities.items():
	print("%s has the city %s"%(abbrev,city))
#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

print("-"*10)
for state , abbrev in states.items():
	print("%s state is abbreviated %s and has city %s"%(state,abbrev,cities[abbrev]))
#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

print("-"*10)
state = states.get("Texas",None)
#get() 函式返回指定鍵的值,如果值不在字典中返回預設值。

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

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

加分習題

1、將每一個被呼叫的函式以上述的方式翻譯成 Python 實際執行的動作。例如: ' '.join(things) 其實是 join(' ', things) 。

2、將這兩種方式翻譯為自然語言。例如, ' '.join(things) 可以翻譯成“用 ‘ ‘ 連線(join) things”,而 join(' ', things) 的意思是“為 ‘ ‘ 和 things 呼叫 join 函式”。這其實是同一件事情。

3、上網閱讀一些關於“面向物件程式設計(Object Oriented Programming)”的資料。暈了吧?嗯,我以前也是。別擔心。你將從這本書學到足夠用的關於面向物件程式設計的基礎知識,而以後你還可以慢慢學到更多。

4、查一下 Python中的 “class” 是什麼東西。不要閱讀關於其他語言的 “class” 的用法,這會讓你更糊塗。

dir(something) 和 something 的 class 有什麼關係?

5、如果你不知道我講的是些什麼東西,別擔心。程式設計師為了顯得自己聰明,於是就發明了 Object Oriented Programming,簡稱為 OOP,然後他們就開始濫用這個東西了。如果你覺得這東西太難,你可以開始學一下 “函式程式設計(functional programming)”。

常見問題回答

你不是說別用 while-loop 嗎?

是的。你要記住,有時候如果你有很好的理由,那麼規則也是可以打破的。死守著規則不放的人是白痴。

stuff[3:5] 實現了什麼功能?

這是一個列表切片動作,它會從 stuff 列表的第 3 個元素開始取值,直到第 5 個元素。注意,這裡並不包含第 5 個元素,這跟 range(3,5) 的情況是一樣的。

為什麼 join(' ', stuff) 不靈?

join 的文件寫得有問題。其實它不是這麼工作的,其實它是你要插入的字串的一個方法函式,函式的引數是你要連線的字串構成的陣列,所以應該寫作 ' '.join(stuff) 。