--参考书目《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.
相关推荐
Python编程学习
Python编程学习
Python 编程学习笔记本
AI Python编程学习课件-第10章Python增强现实编程实践
AI Python编程学习课件-第6章深度学习
AI Python编程学习课件-第9章Python并行计算
AI Python编程学习课件-第8章区块链技术编程实践
Python编程学习初体验
python编程,可以用来学习,提升代码能力,深度了解python原理
Python编程学习初体验.pdf
AI Python编程学习课件-第7章量子计算
AI Python编程学习课件-第4章语音识别
AI Python编程学习课件-第5章自然语言处理
AI Python编程学习课件-第1章绪论 主要内容: 人工智能起源与发展 人工智能的主要应用领域 中国人工智能发展现状 Python与人工智能
基于 Skulpt.js 的在线 Python 编程学习网站
AI Python编程学习课件-第2章数据处理常用算法 主要内容: 傅里叶变换 卷积 二分法求解 最小二乘法 泰勒级数 差分法逼近微分 蒙特卡罗方法 梯度下降算法
AI Python编程学习课件-第3章图像识别算法 主要内容: 图像识别算法简介 OpenCV与视频图像处理 基于ImageAI的图像识别 人脸识别 Tesseract OCR与文本识别 基于百度AI的智能图像识别
【个人总结系列-9】Python编程学习总结.pdf
Python编程学习源码例题资料20220401-3992-V2.docx
【个人总结系列-9】Python编程学习总结.rar