Welcome to 陈俊峰's ---BeetleHeaded Man Blog !

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  58 随笔 :: 32 文章 :: 18 评论 :: 0 Trackbacks

Python Reading Notes : (2006-4-13)
Note One : the powerful Lists

Using Lists as Stacks
 
To add an item to the top of the stack, use append() ,To retrieve an item from the top of the stack, use pop() without an explicit index. For example:
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]

Using Lists as Queues


To add an item to the back of the queue, use append() To retrieve an item from the front of the queue, use pop() with 0 as the index. For example:
>>> queue = ["Eric", "John", "Michael"]
>>> queue.append("Terry")           # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.pop(0)
'Eric'
>>> queue.pop(0)
'John'
>>> queue
['Michael', 'Terry', 'Graham']

Note Two :about Tuples (distinguish between string type and  tuples tpye,especially zero or only one items contained in a tuples )

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective. For example:
>>> empty = ()
>>> singleton = 'hello',    # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)


but if you write a statement like this :
>>> singleton = 'hello'          # it means that you define or construct a string type,not a tuples
>>> singleton
'hello'
 


posted on 2006-04-13 11:26 Jeff-Chen 阅读(151) 评论(0)  编辑 收藏 引用 所属分类: Python

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理