commit/obp.RWPy4learner.v1: ream D: 完成第一章第五节

7 views
Skip to first unread message

Bitbucket

unread,
May 3, 2012, 6:56:23 AM5/3/12
to zoomquiet...@gmail.com
1 new commit in obp.RWPy4learner.v1:


https://bitbucket.org/ZoomQuiet/obp.rwpy4learner.v1/changeset/2ed8309ec57b/
changeset: 2ed8309ec57b
user: ream D
date: 2012-05-03 12:55:40
summary: 完成第一章第五节
affected #: 4 files

diff -r ea37cb81cceac26393a304d9f19600c1a24feff8 -r 2ed8309ec57b350aada08b64e98dc8ed2ae51382 source/_static/snap/Thumbs.db
Binary file source/_static/snap/Thumbs.db has changed


diff -r ea37cb81cceac26393a304d9f19600c1a24feff8 -r 2ed8309ec57b350aada08b64e98dc8ed2ae51382 source/_static/snap/ch01-5-dict.png
Binary file source/_static/snap/ch01-5-dict.png has changed


diff -r ea37cb81cceac26393a304d9f19600c1a24feff8 -r 2ed8309ec57b350aada08b64e98dc8ed2ae51382 source/ch01/index.rst
--- a/source/ch01/index.rst
+++ b/source/ch01/index.rst
@@ -19,9 +19,11 @@
variable_object_mala.rst

useif.rst
-  
+
Usefor.rst

+ usedict.rst
+
suggested_reading.rst

summary.rst


diff -r ea37cb81cceac26393a304d9f19600c1a24feff8 -r 2ed8309ec57b350aada08b64e98dc8ed2ae51382 source/ch01/usedict.rst
--- /dev/null
+++ b/source/ch01/usedict.rst
@@ -0,0 +1,269 @@
+再次优化通信录
+====================
+
+
+“对,前面已经决定要做一个添加联系人的功能,怎样才能实现添加联系人呢?”小白想啊想啊,这时,他突然想到:“对了,前一次优化,是用了 list ,在《简明 Python》 中,list 属于的章节是数据结构,那么数据结构....”小白想起了简明中对数据结构的描述:
+
+::
+
+ “数据结构基本上就是——它们是可以处理一些 数据 的 结构 。或者说,它们是用来存储一组相关数据的。
+
+ 在Python中有三种内建的数据结构——列表、元组和字典。我们将会学习如何使用它们,以及它们如何使编程变得简单”
+
+“对,数据结构就是用来储存某些数据的,那么,我只时用了数据结构中的列表,还有三个数据结构我没有学呢!”小白想“为何我不去数据结构哪一张找找看有没有一些更好的数据结构,可以实现程序的添加联系人功能的呢?”小白想。
+
+小白打开《简明的 Python 教程》中的 数据结构 一节,突然,他眼前一亮,发现了这些内容:
+
+.. image:: ../_static/snap/ch01-5-dict.png
+
+“字典?联系人?这也太巧了吧!怎么他和我想做的是同一件东西”小白继续看下去,当他看到了字典的实例后“Oh,这是神马东西嘛,输出这么乱,完全看不懂,还是自己试试看把它上面的实例打到 IDLE 里看会怎样吧!”
+
+
+小白在 Idle 中输入了实例中的语句,并进行了自己的理解。
+::
+
+ #(实例引用自简明)
+ >>> ab = { 'Swaroop' : 'swar...@byteofpython.info',
+ 'Larry' : 'la...@wall.org',
+ 'Matsumoto' : 'ma...@ruby-lang.org',
+ 'Spammer' : 'spa...@hotmail.com'
+ } #这里应该就是新定义一个字典吧?
+
+ >>> print ab
+ {'Swaroop': 'swar...@byteofpython.info', 'Matsumoto': 'ma...@ruby-lang.org', 'Larry': 'la...@wall.org', 'Spammer': 'spa...@hotmail.com'} #果然新定义了一个字典
+ >>> print "Swaroop's address is %s" % ab['Swaroop'] #咦?print 后面跟了一个 ab['Swaroop'] 难道这是读取 ab 中的 Swaroop?
+ Swaroop's address is swar...@byteofpython.info #好像是输出 Swaroop 后面的信息 swar...@byteofpython.info 了。
+ >>> print ab['Swaroop'] #试试单独打印 ab['Swaroop'] 试试
+ swar...@byteofpython.info #哇塞,果然输出了 ab['Swaroop'] 后面的值,看来只要用 字典名['字典中要读取的键'] 这种格式就能读取到字典中的值了
+ >>> ab['Guido'] = 'gu...@python.org' #这句有事干什么用呢?字典 ab 中并没有 Guido 这个键啊?
+ >>> print ab['Guido'] #打印 Guido ?可字典里面没有这个键啊?
+ gu...@python.org #字典中好像又多了 Guido 这个键了,莫非上一句是在字典中添加 Guido 这个键吗?赶快看看!
+ >>> print ab
+ {'Swaroop': 'swar...@byteofpython.info', 'Matsumoto': 'ma...@ruby-lang.org', 'Larry': 'la...@wall.org', 'Spammer' : 'spa...@hotmail.com','Guido': 'gu...@python.org'} #果然多出了 Guido 这个键,看来那一句果然是用于添加字典中的键值的
+ >>> del ab['Spammer'] #del?删?难道这是删除字典中的某个键吗?
+ >>> print ab['Spammer'] #打印出来看看?
+
+ Traceback (most recent call last): #出错,看来果然是删掉那个键值了
+ File "<pyshell#7>", line 1, in <module>
+ print ab['Spammer']
+ KeyError: 'Spammer'
+ >>>
+
+“哈哈,这个字典真是太好用了,竟然能通过对应的键列出对应的值,太棒了,这样我的通信录又能简化不少啊,连 list 都不用用了,而且看着还清楚,真是太方便了”小白迫不及待想要把他的通信录改成用字典的通信录。“对,咱就先实现字典读取,再实现字典添加,先一步步来。”小白马上动起手来。
+
+::
+
+ _Info = {"张三":54321,
+ "李四":12345}
+ _run = True
+ while _run:
+ _User_input = raw_input("输入联系人名:")
+ print _Info[_User_input]
+
+
+
+小白尝试着运行它的程序:
+
+::
+
+ >>>
+ 输入联系人名:张三
+ 54321
+ 输入联系人名:李四
+ 12345
+ 输入联系人名:王五
+
+ Traceback (most recent call last):
+ File "E:/test.py", line 7, in <module>
+ print _Info[_User_input]
+ KeyError: '\xcd\xf5\xce\xe5'
+ >>>
+
+“对哇,没有作一个判断,以判断用户输入的键是否在字典里,这样的话如果用户输入一个不存在的键的话,程序就会像这样报错了,可是,怎样才能判断字典里面有没有某个键呢?直接读取不存在的键是肯定不行的呀,这样肯定出错!有没有判断一个字典里面有没有某个键的办法呢?”这时,小白想到了一个绝对能找到答案的办法:python 的 help()
+
+“对嘛,前面早就已经知道python有help()这个办法了,为何不用一下呢?”小白在简明中已经知道了字典的是 dict 。“那么,只要 help(dict) 不就可以了吗?”小白马上动手。
+
+::
+
+ >>> help(dict)
+ Help on class dict in module __builtin__:
+
+ class dict(object)
+ | dict() -> new empty dictionary
+ | dict(mapping) -> new dictionary initialized from a mapping object's
+ | (key, value) pairs
+ | dict(iterable) -> new dictionary initialized as if via:
+ | d = {}
+ | for k, v in iterable:
+ | d[k] = v
+ | dict(**kwargs) -> new dictionary initialized with the name=value pairs
+ | in the keyword argument list. For example: dict(one=1, two=2)
+ |
+ | Methods defined here:
+ |
+ | __cmp__(...)
+ | x.__cmp__(y) <==> cmp(x,y)
+ |
+ | __contains__(...)
+ | D.__contains__(k) -> True if D has a key k, else False
+ |
+ | __delitem__(...)
+ | x.__delitem__(y) <==> del x[y]
+ |
+ | __eq__(...)
+ | x.__eq__(y) <==> x==y
+ |
+ | __ge__(...)
+ | x.__ge__(y) <==> x>=y
+ |
+ | __getattribute__(...)
+ | x.__getattribute__('name') <==> x.name
+ |
+ | __getitem__(...)
+ | x.__getitem__(y) <==> x[y]
+ |
+ | __gt__(...)
+ | x.__gt__(y) <==> x>y
+ |
+ | __init__(...)
+ | x.__init__(...) initializes x; see help(type(x)) for signature
+ |
+ | __iter__(...)
+ | x.__iter__() <==> iter(x)
+ |
+ | __le__(...)
+ | x.__le__(y) <==> x<=y
+ |
+ | __len__(...)
+ | x.__len__() <==> len(x)
+ |
+ | __lt__(...)
+ | x.__lt__(y) <==> x<y
+ |
+ | __ne__(...)
+ | x.__ne__(y) <==> x!=y
+ |
+ | __repr__(...)
+ | x.__repr__() <==> repr(x)
+ |
+ | __setitem__(...)
+ | x.__setitem__(i, y) <==> x[i]=y
+ |
+ | __sizeof__(...)
+ | D.__sizeof__() -> size of D in memory, in bytes
+ |
+ | clear(...)
+ | D.clear() -> None. Remove all items from D.
+ |
+ | copy(...)
+ | D.copy() -> a shallow copy of D
+ |
+ | get(...)
+ | D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
+ |
+ | has_key(...)
+ | D.has_key(k) -> True if D has a key k, else False
+ |
+ | items(...)
+ | D.items() -> list of D's (key, value) pairs, as 2-tuples
+ |
+ | iteritems(...)
+ | D.iteritems() -> an iterator over the (key, value) items of D
+ |
+ | iterkeys(...)
+ | D.iterkeys() -> an iterator over the keys of D
+
+小白仔细观察 help 出来的用方法,这时,一个用法吸引了他的注意:
+
+::
+
+ | has_key(...)
+ | D.has_key(k) -> True if D has a key k, else False
+
+“True if D has a key k, else False?如果字典 D 有键 k 就返回 True,否则返回 False?这正是我需要的!看样子,它的用法就是 D.has_key(k) 嘛,马上试一下看到底是不是这样用的!”小白立即对其进行了尝试:
+
+::
+
+ >>> _Info = {"张三":54321,
+ "李四":12345}
+ >>>
+ >>> _Info.has_key("张三")
+ True
+ >>> _Info.has_key("hello")
+ False
+ >>>
+
+“哈哈,真的是这样,接下来在原程序上加个 if 判断不就可以了吗?马上把他融入到我的程序里试试。”小白迅速动作:
+
+::
+
+
+ _Info = {"张三":54321,
+ "李四":12345}
+
+ _run = True
+ while _run:
+ _User_input = raw_input("输入联系人名:")
+ if _Info.has_key(_User_input) == True:
+ print _Info[_User_input]
+ elif _User_input == "退出":
+ _run = False
+ else:
+ print "无此联系人"
+
+“运行试试。”小白对他的程序进行调试。
+
+::
+
+ >>>
+ 输入联系人名:张三
+ 54321
+ 输入联系人名:李四
+ 12345
+ 输入联系人名:王五
+ 无此联系人
+ 输入联系人名:退出
+ >>>
+
+“对,果然成功了,一切都在意料之中,用字典真是太方便了,接下来就是把我已经知道的 ab['Guido'] = 'gu...@python.org' 这个用法融入到程序中就能实现联系人添加功能了”小白迅速进行融入:
+
+::
+
+
+
+ _Info = {"张三":54321,
+ "李四":12345}
+
+ _run = True
+ while _run:
+ _User_input = raw_input("输入联系人名:")
+ if _Info.has_key(_User_input) == True:
+ print _Info[_User_input]
+ elif _User_input == "增加":
+ _New_Name = raw_input("输入要增加的联系人名:")
+ _New_Info = raw_input("输入该联系人的信息:")
+ _Info[_New_Name] = _New_Info
+ elif _User_input == "退出":
+ _run = False
+ else:
+ print "无此联系人"
+
+“写好!调试一下程序看会不会有问题。”小白想。
+
+::
+
+ 输入联系人名:张三
+ 54321
+ 输入联系人名:李四
+ 12345
+ 输入联系人名:王五
+ 无此联系人
+ 输入联系人名:增加
+ 输入要增加的联系人名:王二麻子
+ 输入该联系人的信息:123333
+ 输入联系人名:王二麻子
+ 123333
+ 输入联系人名:退出
+ >>>
+
+“好,程序果然没有问题。诶?如果这样做的话就会有一个 bug 哇,用户退出了程序之后他添加的联系人就全都没了哇!”小白又陷入了沉思......
\ No newline at end of file

Repository URL: https://bitbucket.org/ZoomQuiet/obp.rwpy4learner.v1/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.

ream D

unread,
May 3, 2012, 7:09:18 AM5/3/12
to openboo...@googlegroups.com, zoomquiet...@gmail.com
第一章第五节已经完成。由于是按照新定立的 内容知识概括 写的,所以看起来会和前面的几节不太搭。Jibo 可以顺着我的内容续下去了。

在 12-5-3,Bitbucket<commits...@bitbucket.org> 写道:

> --
> '''邮件来自::"OpenBookProject"-开放图书计划 讨论列表
> 详情: http://groups-beta.google.com/group/OpenBookProject
> 发言: openboo...@googlegroups.com
> 退订: openbookproje...@googlegroups.com
> 维基: http://wiki.woodpecker.org.cn/moin/OpenBookProject
> 工程环境: http://code.google.com/p/openbookproject
> 技术列表: http://groups-beta.google.com/group/python-cn
> '''
>


--
-dreampython.orgs.hk

Reply all
Reply to author
Forward
0 new messages