映射、字典

## 映射类型内建函数
* dict
Error:
Python核心编程(第二版)p170
>>> dict([['x', 1], ['y', 2]])
{'y': 2, 'x': 1}
实际输出测试:
>>> dict([['x', 1], ['y', 2]])
{'y': 2, 'x'Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 0] Error Correct:
>>> dict((['x', 1], ['y', 2]))
{'y': 2, 'x': 1} ## 三元操作符
X if C else Y eg.
>>> x, y = 4, 5
>>> smaller = x if x < y else y
>>> smaller
4 ## enumerate()内建函数
eg.
>>> numlist = ['a', 'b', 'c']
>>> for index, i in enumerate(numlist):
... print '%d %s' % (index+1, i)
...
1 a
2 b
3 c ## zip()
eg.
>>> numlist
['a', 'b', 'c']
>>> strlist = [1, 2, 3]
>>> strlist
[1, 2, 3]
>>> for num, str in zip(numlist, strlist):
... print '%d \t %s' % (str, num)
...
1 a
2 b
3 c ## 再谈else语句
在Python中,else语句也可以在while和for循环中使用。在循环中使用时,else子句只在循环完成后执行,但是break语句会跳过else块。 eg.
>>> for i in range(10, 21):
... if i%2 == 0:
... print '[%d] \t [%d]' % (i, i%2)
... break
... else:
... print 'Break Test successful!'
...
[10] [0] ## 迭代器
* 0、创建迭代器
iter(obj)
iter(func, sentinel) 迭代器是一个有next()方法的对象,通过next()可以取出所需要的下一个项,当所有的项被取出后,就会报一个StopIteration异常,这并不是一个错误,只是告诉外部调用者,迭代完成。
* 1、序列
一个for循环的完整工作是这样的:
>>> seq = ('q121', 132, 'dad')
>>> seq = iter(seq)
>>> while True:
... try:
... i = seq.next()
... except StopIteration:
... break
... print '[%s]' % i
...
[q121]
[132]
[dad] * 2、字典
eg.dict.iterkeys() dict.itervalues() dict.iteritems()
>>> seq = {'a': 1, 'b': 2}
>>> for i in seq.iterkeys():
... print i
...
a
b >>> for i in seq.values():
... print i
...
1
2 >>> for i in seq.items():
... print i
...
('a', 1)
('b', 2)
>>> for i, j in seq.items():
... print '[%s] \t [%s]' % (i, j)
...
[a] [1]
[b] [2] ## 列表表达式
eg.
>>> lambda x: x ** 2, range(6)
(<function <lambda> at 0x00000000052AEBA8>, [0, 1, 2, 3, 4, 5])
>>> map(lambda x: x ** 2, range(6))
[0, 1, 4, 9, 16, 25]
>>> [x ** 2 for x in range(6)]
[0, 1, 4, 9, 16, 25]
>>> [x ** 2 for x in range(6) if x % 2 == 0]
[0, 4, 16] 列表解析式的判断部分默认为真:
eg.
>>> [x ** 2 for x in range(6) if x % 2 ] // 真
[1, 9, 25]
>>> [x ** 2 for x in range(6) if x % 2 == 0] // 假
[0, 4, 16] ## 矩阵样例
同一个例子,为啥输出不一样!
eg.
>>> [(x+1, y+1) for x in range(3) for y in range(5)]
[(1, 1)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 0] Error
>>> [(x+1, y+1) for x in range(3) for y in range(5)]
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5)] ## 追加
>>> str
['suahduaihsdu']
>>> [i for word in str for i in word]
['s', 'u', 'a', 'h', 'd', 'u', 'a', 'i', 'h', 's', 'd', 'u'] ## os.stat()
eg.
>>> import os
>>> print os.stat("/root/python/zip.py")
(33188, 2033080, 26626L, 1, 0, 0, 864, 1297653596, 1275528102, 1292892895)
>>> print os.stat("/root/python/zip.py").st_mode #权限模式
33188
>>> print os.stat("/root/python/zip.py").st_ino #inode number
2033080
>>> print os.stat("/root/python/zip.py").st_dev #device
26626
>>> print os.stat("/root/python/zip.py").st_nlink #number of hard links
1
>>> print os.stat("/root/python/zip.py").st_uid #所有用户的user id
0
>>> print os.stat("/root/python/zip.py").st_gid #所有用户的group id
0
>>> print os.stat("/root/python/zip.py").st_size #文件的大小,以位为单位
864
>>> print os.stat("/root/python/zip.py").st_atime #文件最后访问时间
1297653596
>>> print os.stat("/root/python/zip.py").st_mtime #文件最后修改时间
1275528102
>>> print os.stat("/root/python/zip.py").st_ctime #文件创建时间
1292892895

最新文章

  1. QWhatsThis的用法
  2. 关于更新发布CSS和JS文件的缓存问题
  3. (转)对比MS Test与NUnit Test框架
  4. [DataBase] MongoDB (8) 副本集
  5. python 核心编程课后练习(chapter 2)
  6. jQuery事件对象event的属性和方法
  7. URLConnection 和 HttpClients 发送请求范例
  8. Angular学习(5)- 数组双向梆定+filter
  9. jQuery技术内幕预览版.pdf2
  10. 屌丝程序猿赚钱之道 之APP
  11. 字符串查找 strstr
  12. IOS后台执行机制 与 动作
  13. 测者的测试技术手册:测试应该关注java.util.List.subList的坑
  14. js datagrid 移动去重
  15. luogu 1314 聪明的质检员
  16. fputcsv导出大量数据
  17. [转]Angular4 自制分页控件
  18. oracle之 手动创建 emp 表 与 dept 表
  19. vue-cli 中的 webpack 配置详解
  20. CF1039D You Are Given a Tree 根号分治,贪心

热门文章

  1. python 并发之多进程实现
  2. mock static方法
  3. 数据插入INSERT
  4. android上最多有多少个http连接?
  5. 关于表格合并span-method方法的补充(表格数据由后台动态返回)
  6. 使用JWT来实现单点登录功能
  7. oracle 单实例DG(配置篇二)
  8. VIRTIO概述和基本原理
  9. 剑指offer第3题:从尾到头打印链表
  10. 一个典型案例为你解读TDSQL 全时态数据库系统