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

Python基础之:List的更多相关文章

  1. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

  2. Python开发【第二篇】:Python基础知识

    Python基础知识 一.初识基本数据类型 类型: int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位 ...

  3. Python小白的发展之路之Python基础(一)

    Python基础部分1: 1.Python简介 2.Python 2 or 3,两者的主要区别 3.Python解释器 4.安装Python 5.第一个Python程序 Hello World 6.P ...

  4. Python之路3【第一篇】Python基础

    本节内容 Python简介 Python安装 第一个Python程序 编程语言的分类 Python简介 1.Python的由来 python的创始人为吉多·范罗苏姆(Guido van Rossum) ...

  5. 进击的Python【第三章】:Python基础(三)

    Python基础(三) 本章内容 集合的概念与操作 文件的操作 函数的特点与用法 参数与局部变量 return返回值的概念 递归的基本含义 函数式编程介绍 高阶函数的概念 一.集合的概念与操作 集合( ...

  6. 进击的Python【第二章】:Python基础(二)

    Python基础(二) 本章内容 数据类型 数据运算 列表与元组的基本操作 字典的基本操作 字符编码与转码 模块初探 练习:购物车程序 一.数据类型 Python有五个标准的数据类型: Numbers ...

  7. Python之路【第一篇】python基础

    一.python开发 1.开发: 1)高级语言:python .Java .PHP. C#  Go ruby  c++  ===>字节码 2)低级语言:c .汇编 2.语言之间的对比: 1)py ...

  8. python基础之day1

    Python 简介 Python是著名的“龟叔”Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言. Python为我们提供了非常完善的基础代码库,覆盖了 ...

  9. python基础之文件读写

    python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使用os模块的一些方法如下: 得到 ...

  10. python基础之编码问题

    python基础之编码问题 本节内容 字符串编码问题由来 字符串编码解决方案 1.字符串编码问题由来 由于字符串编码是从ascii--->unicode--->utf-8(utf-16和u ...

随机推荐

  1. Mutex

    #include "stdafx.h" #include <string> #include <iostream> #include <Windows ...

  2. 如何在tpl模版的div块中加ztree

    ld-ztree.tpl <div class="ld-ztree-container"> <div class="ld-ztree-header te ...

  3. java笔试面试二

    http://www.cnblogs.com/lanxuezaipiao/p/3371224.html

  4. C#打开文件对话框

    OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = System.Environment.CurrentDirector ...

  5. 逻辑运算符&&和&的区别 ||和|的区别

    A:最终结果一样. B:&& 和 || 有短路作用,左边是false ,右边不执行.

  6. Nginx的安装配置 例子

    1.下载 2.解压 3.运行 a.双击nginx.bat b.启动Nginx 会发现进程里面已经开始运行 4.配置 a.双击打开配置文件夹里面的nginx.conf b.修改 upstream tee ...

  7. 使用JQuery Ajax请求,在Controller里获取Session

    昨天在做项目的时候,两个平台之间的切换,虽然两个网站的Session都指向了同一台机子,但是通过Ajax方式来请求时,就是不能获取到Session的值. 在调试的过程中发现,原来是Session的Is ...

  8. iOS7——UIControlEventTouchDown延迟响应问题

    问题描述 在iOS7下开发,真机调试时,UIButton的其他事件响应都正常,但是UIControlEventTouchDown事件响应会延迟,而且不同响应区域发生的延时情况不同,有时延迟1s以后响应 ...

  9. Python:关于字典的相关操作

    >>> people = {"Tom":170, "Jack":175, "Kite":160, "White& ...

  10. eclipse重定向输入输出到文件

    最近在学习算法第四版,为了要用作者给的测试数据alg4-data,需要将数据直接导入到程序中.在作者的示例代码里用了重定向来做这个事情,但是在eclipse里使用重定向很不方便,查了很多资料,都说是在 ...