笨方法学Python 习题 38: 列表的操作

本文通过解析Python列表操作,如' '.join(things)与join(' ', things),引导读者理解面向对象编程的基本概念。同时,鼓励读者查阅Python中的"class"并探讨dir()函数与对象类的关系。此外,讨论了列表切片和join方法的正确用法,以及何时可以打破编程规则的灵活运用。" 112891567,10535031,Java实现带干扰线的图片验证码生成,"['Java', '图形处理', '图像生成', '验证码技术']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!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) 。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值