Note_python(01)

映射、字典

映射類型內建函數

  • 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
發佈了55 篇原創文章 · 獲贊 20 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章