Python基础之:List
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的更多相关文章
- python之最强王者(2)——python基础语法
背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...
- Python开发【第二篇】:Python基础知识
Python基础知识 一.初识基本数据类型 类型: int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位 ...
- Python小白的发展之路之Python基础(一)
Python基础部分1: 1.Python简介 2.Python 2 or 3,两者的主要区别 3.Python解释器 4.安装Python 5.第一个Python程序 Hello World 6.P ...
- Python之路3【第一篇】Python基础
本节内容 Python简介 Python安装 第一个Python程序 编程语言的分类 Python简介 1.Python的由来 python的创始人为吉多·范罗苏姆(Guido van Rossum) ...
- 进击的Python【第三章】:Python基础(三)
Python基础(三) 本章内容 集合的概念与操作 文件的操作 函数的特点与用法 参数与局部变量 return返回值的概念 递归的基本含义 函数式编程介绍 高阶函数的概念 一.集合的概念与操作 集合( ...
- 进击的Python【第二章】:Python基础(二)
Python基础(二) 本章内容 数据类型 数据运算 列表与元组的基本操作 字典的基本操作 字符编码与转码 模块初探 练习:购物车程序 一.数据类型 Python有五个标准的数据类型: Numbers ...
- Python之路【第一篇】python基础
一.python开发 1.开发: 1)高级语言:python .Java .PHP. C# Go ruby c++ ===>字节码 2)低级语言:c .汇编 2.语言之间的对比: 1)py ...
- python基础之day1
Python 简介 Python是著名的“龟叔”Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言. Python为我们提供了非常完善的基础代码库,覆盖了 ...
- python基础之文件读写
python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使用os模块的一些方法如下: 得到 ...
- python基础之编码问题
python基础之编码问题 本节内容 字符串编码问题由来 字符串编码解决方案 1.字符串编码问题由来 由于字符串编码是从ascii--->unicode--->utf-8(utf-16和u ...
随机推荐
- 数据库—SQL语句
下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DEL ...
- MCV之行为
在Controller中的方法都称为行为,所以的公共方法都可以在浏览器中调用,返回值为:ActionResult的类型或其子类,这个类为抽象类,所以这为抽象编程,方法的结果返回为直接或间接继承自Act ...
- JS数组转成json字符串的注意事项
在js中常常会将一个数组转成json字符串发送给后端. 这时候在定义数组数据结构的时候需要格外注意,意味json中是有集合和对象的区别的. 集合的定义是[];对象的的定义是{}. 这时候,在创建数组时 ...
- vs2013单元测试练习过程
1.打开VS2013 --> 新建一个项目.这里我们默认创建一个控制台项目.取名为UnitTestDemo 2.在解决方案里面新增一个单元测试项目.取名为UnitTestDemoTest 创建完 ...
- linux 关于用户与组的操作
1.添加用户: useradd handongyu 2.查看所有用户 cat /etc/passwd 查看某一用户用 cat /etc/passwd |grep root 3.查看所有组 ca ...
- Generate transparent shape on image
Here is an example code to generate transparent shape on image. Need to pay attention can not use cv ...
- 怎样增强MyEclipse的代码自动提示功能
步骤/方法 1 一 般在Eclipse ,MyEclipse代码里面,打个foreach,switch等 这些,是无法得到代码提示的(不信自己试试),其他的就更不用说了,而在Microsoft Vis ...
- 用原生js写碰撞变色效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 重走java---Step 1
开发环境 1.使用java开发,首先要完成java运行环境的安装配置,JVM可以说是java最大的优点之一,就是它实现了java一次编译多次运行,关于JVM以后再详谈.安装配置JDK,完成java开发 ...
- Spark 2.6.1 源代码在 eclipse 的配置
本文地址:http://www.cnblogs.com/jying/p/3671767.html 这么个问题又耗费了偶一天时间,真是羞愧.. 上午从官网svn地址下载最新的 spark 包,总是下载失 ...