Python——追加学习笔记(一)

映射、字典

## 映射类型内建函数
* 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
posted @ 2017-12-12 19:03  水木梵华  阅读(336)  评论(0编辑  收藏  举报