python中文文档阅读笔记2
1 字典类型适合序列类型相对立的,他的key不能是可变类型一般是数字,元组和字符串,注意,这里的元组包含可变类型也不可以。
2 当字典的key是字符串的时候,使用关键字参数和建立一个dict的好方法。
3 iteritems()遍历字典,enumerate()遍历序列,zip()函数能在循环的时候将多个序列的相应位置的元组进行配对:
>>> questions = ['name', 'quest', 'favorite color'] >>> answers = ['lancelot', 'the holy grail', 'blue'] >>> for q, a in zip(questions, answers): ... print 'What is your {0}? It is {1}.'.format(q, a) ... What is your name? It is lancelot. What is your quest? It is the holy grail. What is your favorite color? It is blue.
4 在python中赋值操作必须是单独的不能再表达式内部,这样防止了类似于本应==符号写成了=符号的失误:while a=1: 出错。
5 比较不同类型对象是的结果是确定的,但也是任意的,比较同样类型的结果是按照字典序进行比较的。

浙公网安备 33010602011771号