Python:List (列表)

list 为Python内建类型,位于__builtin__模块中,元素类型可不同,元素可重复,以下通过实际操作来说明list的诸多功能,主要分为增、删、改、查

list帮助:在IDE中输入 help(list)可查看

Help on class list in module __builtin__:

class list(object)
| list() -> new empty list
| list(iterable) -> new list initialized from iterable's items
|
| Methods defined here:
|
| __add__(...)
| x.__add__(y) <==> x+y
|
| __contains__(...)
| x.__contains__(y) <==> y in x
|
| __delitem__(...)
| x.__delitem__(y) <==> del x[y]
|
| __delslice__(...)
| x.__delslice__(i, j) <==> del x[i:j]
|
| Use of negative indices is not supported.
|
| __eq__(...)
| x.__eq__(y) <==> x==y
|
| __ge__(...)
| x.__ge__(y) <==> x>=y
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __getslice__(...)
| x.__getslice__(i, j) <==> x[i:j]
|
| Use of negative indices is not supported.
|
| __gt__(...)
| x.__gt__(y) <==> x>y
|
| __iadd__(...)
| x.__iadd__(y) <==> x+=y
|
| __imul__(...)
| x.__imul__(y) <==> x*=y
|
| __init__(...)
| x.__init__(...) initializes x; see help(type(x)) for signature
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __le__(...)
| x.__le__(y) <==> x<=y
|
| __len__(...)
| x.__len__() <==> len(x)
|
| __lt__(...)
| x.__lt__(y) <==> x x*n
|
| __ne__(...)
| x.__ne__(y) <==> x!=y
|
| __repr__(...)
| x.__repr__() <==> repr(x)
|
| __reversed__(...)
| L.__reversed__() -- return a reverse iterator over the list
|
| __rmul__(...)
| x.__rmul__(n) <==> n*x
|
| __setitem__(...)
| x.__setitem__(i, y) <==> x[i]=y
|
| __setslice__(...)
| x.__setslice__(i, j, y) <==> x[i:j]=y
|
| Use of negative indices is not supported.
|
| __sizeof__(...)
| L.__sizeof__() -- size of L in memory, in bytes
|
| append(...)
| L.append(object) -- append object to end
|
| count(...)
| L.count(value) -> integer -- return number of occurrences of value
|
| extend(...)
| L.extend(iterable) -- extend list by appending elements from the iterable
|
| index(...)
| L.index(value, [start, [stop]]) -> integer -- return first index of value.
| Raises ValueError if the value is not present.
|
| insert(...)
| L.insert(index, object) -- insert object before index
|
| pop(...)
| L.pop([index]) -> item -- remove and return item at index (default last).
| Raises IndexError if list is empty or index is out of range.
|
| remove(...)
| L.remove(value) -- remove first occurrence of value.
| Raises ValueError if the value is not present.
|
| reverse(...)
| L.reverse() -- reverse *IN PLACE*
|
| sort(...)
| L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
| cmp(x, y) -> -1, 0, 1
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
|
| __new__ =
| T.__new__(S, ...) -> a new object with type S, a subtype of T

list帮助

1)增

a.初始化

直接用 list1 = list()或者 list1 =[ ]构造一个空列表

list2=[1,2,3] , list3=list(list2) 两种方式构造非空列表

b.追加元素

list2.append(5)  --> [1,2,3,5],追加单个元素

list2.extend(['a','b']) --> [1,2,3,5,'a','b'] 追加后面列表中所有元素

list2.insert(0,'begin') --> ['begin',1,2,3,5,'a','b'] 指定位置插入元素

2)删

list2.remove('a') --> ['begin',1,2,3,5,'b'] 删除列表中指定值,只删除第一次出现的指定值,若指定值不在列表中则抛出ValueError

list2.pop(1) --> ['begin',2,3,5,'b'] 删除指定索引处的值并返回,若列表为空或索引值超出范围则抛出IndexError,默认(无参情况下)删除最后一个元素并返回,利用list.pop()可实现栈操作

3)查

list2[0] --> 根据index查找元素 ,支持切片 ,如list[0:3] --> ['begin',2,3]

'begin' in / not in list2 --> 查看某元素是否在列表中,返回布尔值

list2.count('b') --> 返回某元素在列表中出现次数

4) 改

list[0]='end' --> 替换指定index索引处元素值

5) 其他操作

反转 ,list2.reverse()  --> 将list2 元素顺序倒转

排序,list2.sort() --> 将list2 元素进行升序排序,或者用sorted(list2)进行排序

实例:

1、用list实现栈

栈原则,先进后出FILO。

 #! /usr/bin/env python
# -*-coding:utf-8-*- class Stack(object): def __init__(self):
self.st = list() def in_stack(self,x):
self.st.append(x)
print 'element %s add to stack...'% x def out_stack(self):
print 'element get out of stack...'
print self.st.pop() def get_top(self):
print self.st[len(st)-1] def get_info(self):
return 'stack is :',self.st,' depth: ',len(self.st)
if __name__=='__main__':
ST = Stack()
ST.in_stack('a')
ST.in_stack('b')
ST.in_stack('c')
ST.in_stack('d')
ST.in_stack('e')
print 'original stack: ',ST.get_info()
ST.out_stack()
ST.out_stack()
print 'stack now is: ',ST.get_info()

Stack

最新文章

  1. Android:开发环境搭建相关问题
  2. Ambari server:无法显示内存,CPU等使用率
  3. jq图片点击居中放大原始图片兼容ie
  4. HTTP 错误 500.21 - Internal Server Error 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler”
  5. jquery 按钮效果 正常、移上、按下
  6. iptables 开启3306端口
  7. StringUtil
  8. protobuf-3.0.0-beta-2 windows编译 x64/x86
  9. PowerStack
  10. cocos2dx_moveby_n_moveto
  11. eclipse的svn插件安装方式
  12. Linux为用户设定环境变量
  13. ossim
  14. QT完美转换特殊字符的大小写
  15. Web Worker Best Practices
  16. 用StyleCop规范团队代码
  17. webpack学习笔记(五)
  18. console输出彩色字体
  19. sql注入--access
  20. 【Unity】10.2 通用动画的导入和设置

热门文章

  1. SQLServer 事务隔离级别
  2. Android基础开发文档汇总
  3. Apache RewriteHTTPToHTTPS
  4. Linux环境下安装Oracle 10g 发生错误 You do not have permission to write to the inventory location
  5. Windows程序设再读笔记01-起步
  6. OAF屏蔽的错误
  7. 线程+IO流
  8. Jenkins自动部署Tomcat项目
  9. 怎样增强MyEclipse的代码自动提示功能
  10. [小哥Allegro72讲速成视频]