--参考书目《Programming Python》(3rd Edition by Mark Lutz)
Chapter2-----A Sneak Preview
这章旨在通过构建一个数据库来熟悉python操作和一些思想
Step 1:Representing Records
主要介绍了两种方法,list 和 dictionary,下逐一介绍
Using Lists:
>>> XuHang = ['Mouse',19,'NJU','Software Engineering']
>>> XuYi = ['Duck',19,'HNU','Electronical Engineering']
>>> ZhangZixuan = ['Black',19,'HBIU','Math']
>>> LiXu = ['Diaosi',19,'HIT','Astronautics Engineering']
The above four recods have 4 properties:nickname,age,university and major.By using the operation of list,it’s easy to access to their fileds.
>>> LiXu[0] #to get nickname
'Diaosi'
furthermore, wo combine them into a unit to form a small database.
>>> people = [XuHang,XuYi,ZhangZixuan,LiXu]
>>> for person in people:
print (person)
['Mouse', 19, 'NJU', 'Software Engineering']
['Duck', 19, 'HNU', 'Electronical Engineering']
['Black', 19, 'HBIU', 'Math']
['Diaosi', 19, 'HIT', 'Astronautics Engineering']
>>> university = [person[2] for person in people]
>>> university
['NJU', 'HNU', 'HBIU', 'HIT']
>>> university = map((lambda x:x[2]),people)
>>> university
['NJU', 'HNU', 'HBIU', 'HIT']
#这里注意下在python3.3中,print函数必须加括号,而在2.7中则不必
By indexing, we can easily process them. What’s more powerful tools are list comprehensions(列表推导),maps.
#对于map这里同样也有版本的区别,在3.3和2.7中分别键入type(map)可以发现在2.7中: ‘builtin_function_or_method’,在3.3中则为’type’,这表明在3.3中将map从函数变为了类,其返回值也不是list,而是iterators,所以在3.3中的使用方法为:
>>> university = map((lambda x:x[2]),people)
>>> list(university)
['NJU', 'HNU', 'HBIU', 'HIT']
将iterator转为list即可
map还有些用法类似于并行处理:
>>> def abc(a,b,c):
return a*10000+b*100+c
>>> list1 = [11,22,33]
>>> list2 = [44,55,66]
>>> list3 = [77,88,99]
>>> list(map(abc,list1,list2,list3))
[114477, 225588, 336699]
#另外一点即是lambda,用于创建一个匿名函数
>>> h = lambda x:x+2
>>> h(5)
7
向database list添加项只需调用append或extend即可
Dificiencies:(1)建立的4个records只是临时储存的对象,一旦退出python对象就会被回收
(2)通过position查找相关properties使代码变得不好阅读,并且每一次查询都必须重复输入上面的操作
Filed Labels:
To better understand what the code means,we associate a field name with a field value..
>>> NICKNAME,AGE,UNI,MAJOR = range(4)
>>> XuHang[MAJOR]
'Software Engineering'
>>> AGE,XuYi[AGE]
(1, 19)
But in this method, we have to remember to update the range assignments whenever we change record structure.Moreover,there is no direct mapping from a record list back to its field’s names,that is you can change 19 to label AGE.
if we try another way:
>>> XuHang = [['nickname','Mouse'],['age','19'],['university','NJU'],['major','Software Engineering']]
>>> ZhangZixuan = [['nickname','Black'],['age',19],['universit','HBIU'],['major','Math']]
>>> XuHang = [['nickname','Mouse'],['age',19],['university','NJU'],['major','Software Engineering']]
>>> people = [XuHang,ZhangZixuan]
>>> for person in people:
for (nickname,value) in person:
if nickname=='nickname':
print (value)
Mouse
Black
We can avoid indexing the concrete numbers.What’s more:
>>> def field(record,label):
for (fname,fvalue) in record:
if (fname == label):
return fvalue
>>> field(ZhangZixuan,'major')
'Math'
By designing such interacting function, we can easily achieve index operations.