免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1856 | 回复: 0
打印 上一主题 下一主题

list的深入学习 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-03 23:08 |只看该作者 |倒序浏览

5.1 More on Lists
The list data type has some more methods.  Here are all of the methods
of list objects:
  append(
  x)
Add an item to the end of the list;
equivalent to a[len(a):] = [x].
  extend(
  L)
Extend the list by appending all the items in the given list;
equivalent to a[len(a):] = L.
  insert(
  i, x)
Insert an item at a given position.  The first argument is the index
of the element before which to insert, so a.insert(0, x)
inserts at the front of the list, and a.insert(len(a), x)
is equivalent to a.append(x).
  remove(
  x)
Remove the first item from the list whose value is x.
It is an error if there is no such item.
  pop(
  )
Remove the item at the given position in the list, and return it.  If
no index is specified, a.pop() removes and returns the last item
in the list.  The item is also removed from the list.  (The square brackets
around the i in the method signature denote that the parameter
is optional, not that you should type square brackets at that
position.  You will see this notation frequently in the
Python Library Reference
.)
  index(
  x)
Return the index in the list of the first item whose value is x.
It is an error if there is no such item.
  count(
  x)
Return the number of times x appears in the list.
  sort(
  )
Sort the items of the list, in place.
  reverse(
  )
Reverse the elements of the list, in place.
An example that uses most of the list methods:
>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print a.count(333), a.count(66.25), a.count('x')
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
5.1.1 Using Lists as Stacks
The list methods make it very easy to use a list as a stack, where the
last element added is the first element retrieved (``last-in,
first-out'').  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]
5.1.2 Using Lists as Queues
You can also use a list conveniently as a queue, where the first
element added is the first element retrieved (``first-in,
first-out'').  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']
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/12592/showart_136066.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP