列表方法

=

Python 3.5.2 (default, Sep 14 2016, 11:27:58)
[GCC 6.2.1 20160901 (Red Hat 6.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> L=['good','boy','!']
>>> L
['good', 'boy', '!']
>>> L.
L.__add__( L.__format__( L.__init__( L.__reduce__( L.__str__( L.insert(
L.__class__( L.__ge__( L.__iter__( L.__reduce_ex__( L.__subclasshook__( L.pop(
L.__contains__( L.__getattribute__( L.__le__( L.__repr__( L.append( L.remove(
L.__delattr__( L.__getitem__( L.__len__( L.__reversed__( L.clear( L.reverse(
L.__delitem__( L.__gt__( L.__lt__( L.__rmul__( L.copy( L.sort(
L.__dir__( L.__hash__ L.__mul__( L.__setattr__( L.count(
L.__doc__ L.__iadd__( L.__ne__( L.__setitem__( L.extend(
L.__eq__( L.__imul__( L.__new__( L.__sizeof__( L.index(

1、append

  append方法用于在列表末尾追加新的对象:

>>> L=['index0','index1','index2']
>>> L
['index0', 'index1', 'index2']
>>> L.append('index3')
>>> L
['index0', 'index1', 'index2', 'index3']
>>> L.append([1,2,3])
>>> L
['index0', 'index1', 'index2', 'index3', [1, 2, 3]]
>>> L.append(('Hello','World','!'))
>>> L
['index0', 'index1', 'index2', 'index3', [1, 2, 3], ('Hello', 'World', '!')]
>>> L.append('index5')
>>> L
['index0', 'index1', 'index2', 'index3', [1, 2, 3], ('Hello', 'World', '!'), 'index5']
>>> len(L)
7
>>> L.append()
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
L.append()
TypeError: append() takes exactly one argument (0 given)
>>> L.append(None)
>>> L
['index0', 'index1', 'index2', 'index3', [1, 2, 3], ('Hello', 'World', '!'), 'index5', None]
>>> L.append([])
>>> L
['index0', 'index1', 'index2', 'index3', [1, 2, 3], ('Hello', 'World', '!'), 'index5', None, []]
>>> id(L)
19215544
>>> L.append(1231)
>>> L
['index0', 'index1', 'index2', 'index3', [1, 2, 3], ('Hello', 'World', '!'), 'index5', None, [], 1231]
>>> id(L)
19215544
>>>

2、count

  count方法统计某个元素在列表中出现的次数:

>>> help(L.count)
Help on built-in function count: count(...)
L.count(value) -> integer -- return number of occurrences of value >>> L=['to','be','or','not','to','be']
>>> L
['to', 'be', 'or', 'not', 'to', 'be']
>>> L.count('to')
2
>>> x=[[1,2],1,1,[1,2,1,[1,2]]]
>>> x.count(1)
2>>> x.count([1,2])
1
>>>

3、extend  

  官方说明:

>>> help(L.extend)
Help on built-in function extend: extend(...)
L.extend(iterable) -> None -- extend list by appending elements from the iterable

  extend方法可以在列表的末尾一次性追加别一个序列中的多个值;换句话说,可以用新列表扩展原有的列表:

>>> a=[1,2,3,4]
>>> id(a)
20761256
>>> b=[5,6,7,8,9]
>>> id(b)
20760536
>>> c=a.extend(b)
>>> c
>>> print(c)
None
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9]
>>> b
[5, 6, 7, 8, 9]
\
>>> id(a)
20761256
>>> b.extend(a)
>>> b
[5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9]
>>>
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9]
>>> b
[5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9]

4、index

  index方法用于从列表中找出某个值第一个匹配项的索引位置:

  举例:

>>> s='He who commences many things finishes but a few'
>>> list1=s.split()
>>> list1
['He', 'who', 'commences', 'many', 'things', 'finishes', 'but', 'a', 'few']
>>> list1.index('who')
1
>>> list1.index('Few')
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
list1.index('Few')
ValueError: 'Few' is not in list
>>> list1.index('who',2)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
list1.index('who',2)
ValueError: 'who' is not in list
>>> list1.index('but',2)
6
>>> list1.index('things',4,5)
4
>>> list1.index('things',5,len(list1))
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
list1.index('things',5,len(list1))
ValueError: 'things' is not in list
>>> list1.index('things',3,len(list1))
4
>>> list1.index('a',-2,-1)
7
>>> len(list1)
9
>>>

5、insert

  insert方法用于将对象插入到列表中;

>>> list1
['He', 'commences', 'many', 'things', 'finishes', 'but', 'a', 'few']
>>> list1.insert(1,'who')
>>> list1
['He', 'who', 'commences', 'many', 'things', 'finishes', 'but', 'a', 'few']
>>> list1.insert(0,'who')
>>> list1
['who', 'He', 'who', 'commences', 'many', 'things', 'finishes', 'but', 'a', 'few']
>>>

6、pop

  pop方法会移除列表中的一个元素(默认是最后一个),并且返回该元素的值:

>>> x=[1,2,3,4]
>>> x.pop()
4
>>> x
[1, 2, 3]
>>> x.pop(0)
1
>>> x
[2, 3]
>>> x.pop(1)
3
>>> x
[2]
>>> help(x.pop)
Help on built-in function pop: 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. >>>

注意: pop方法是唯一一个既能修改列表又返回元素(除了None)的列表方法。

7、remove

  remove方法用于移除列表中某个值的第一个匹配项:

>>> x
['to', 'be', 'or', 'not', 'to', 'be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']
>>> print(x.remove('bee'))
Traceback (most recent call last):
File "<pyshell#65>", line 1, in <module>
print(x.remove('bee'))
ValueError: list.remove(x): x not in list
>>> print(x.remove('to'))
None
>>> x
['or', 'not', 'to', 'be']
>>> x.insert(0,'to')
>>> x
['to', 'or', 'not', 'to', 'be']
>>> x.remove()
Traceback (most recent call last):
File "<pyshell#70>", line 1, in <module>
x.remove()
TypeError: remove() takes exactly one argument (0 given)
>>>

8、reverse

  reverse方法将列表中的元素反向存放;

>>> help(list.reverse)
Help on method_descriptor: reverse(...)
L.reverse() -- reverse *IN PLACE* >>> x=[1,2,3,4]
>>> x
[1, 2, 3, 4]
>>> x.reverse()
>>> x
[4, 3, 2, 1]
>>> x.reverse()
>>> x
[1, 2, 3, 4]
>>>

9、sort

sort方法用于在原位置对列表进行排序,在‘原位置排序’意味着改变原来的列表,从而让其中的元素能按一定的顺序排列,而不是简单地返回一个已经排序

的列表的副本;

>>> x=[2,55,123,89,-2,23]
>>> x.sort()
>>> x
[-2, 2, 23, 55, 89, 123]
>>> x.sort()
>>> x
[-2, 2, 23, 55, 89, 123]

sort方法修改原列表并返回了空值(None):

示例:

>>> help(list.sort)
Help on method_descriptor: sort(...)
L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* >>> x=[23,44,1,5,-100]
>>> y=x.sort() #千万不要这样做!
>>> x
[-100, 1, 5, 23, 44]
>>> y
>>> print(y)
None
>>>

那么当用户需要一个排好序的列表副本,同时又保留原来列表不变的时候,问题就出现了,为了实现这个功能的正解方法是,首先把x的副本赋值给y,然后对y进行排序,

>>> x=[23,12,9,4,1231]
>>> x
[23, 12, 9, 4, 1231]
>>> y=x[:]
>>> y
[23, 12, 9, 4, 1231]
>>> id(x)
20712184
>>> id(y)
20711584
>>> z=x
>>> id(z)
20712184
>>> y.sort()
>>> x
[23, 12, 9, 4, 1231]
>>> y
[4, 9, 12, 23, 1231]
>>>

  再次调用x[:]得到的是包含了x所有元素的分片,这是一种很有效率的复制整个列表的方法

假如只是简单把x赋值给y是没用的,因为这样做让x和y都指向同一个列表了。

Python基础学习-列表的常用方法的更多相关文章

  1. Python基础学习 -- 列表与元组

    本节学习目的: 掌握数据结构中的列表和元组 应用场景: 编程 = 算法 + 数据结构 数据结构: 通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合,这些元素可以是数字或者字符,或者其他数据 ...

  2. Python基础学习-列表基本操作

     列表:Python的“苦力”.   列表不同于元组和字条串的地方:列表是可变的——可以改变列表的内容,并且列表有很多有用的.专门的方法. 1.list函数 因为字符串不能像列表一样被修改,所有有时根 ...

  3. Python基础学习----字符串的常用方法

    # Python字符串 # 大多数的语言定义字符串是双引号,Python既可以双引号,也可以单引号.但使用也有区别 # 单双引号的使用 My_name="bai-boy" Demo ...

  4. Python基础学习----列表

      name_list=["张无忌","张三丰","张小明","胡歌","夏东海"] #循环输出na ...

  5. Python入门基础学习(列表/元组/字典/集合)

    Python基础学习笔记(二) 列表list---[ ](打了激素的数组,可以放入混合类型) list1 = [1,2,'请多指教',0.5] 公共的功能: len(list1) #/获取元素 lis ...

  6. Python基础与科学计算常用方法

    Python基础与科学计算常用方法 本文使用的是Jupyter Notebook,Python3.你可以将代码直接复制到Jupyter Notebook中运行,以便更好的学习. 导入所需要的头文件 i ...

  7. Day1 Python基础学习

    一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编写程序,本质仍然是直接操作 ...

  8. 0003.5-20180422-自动化第四章-python基础学习笔记--脚本

    0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...

  9. Day1 Python基础学习——概述、基本数据类型、流程控制

    一.Python基础学习 一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编 ...

随机推荐

  1. CPU和微架构的概念

    CPU是什么: 中央处理器(CPU,Central Processing Unit)是一块超大规模的集成电路,是一台计算机的运算核心(Core)和控制核心( Control Unit). 它的功能主要 ...

  2. POJ1185炮兵阵地(状态压缩DP)

    POJ飞翔.数据弱 ZQOJ飞翔 数据强 Description 司令部的将军们打算在N×M的网格地图上部署他们的炮兵部队.一个N×M的地图由N行M列组成,地图的每一格可能是山地(用"H&q ...

  3. PS常用快捷键大全

    察看图像类别 说明:: --- Shift键 : --- 空格键         *--- 在Imageready中不适用 § --- 只在Imageready中可用 动作 结果 双击工具箱::或Ct ...

  4. Go语言基础之21--反射

    一.变量介绍 1.1 变量的内在机制 A. 类型信息,这部分是元信息,是预先定义好的:比如:string.int等 B. 值类型,这部分是程序运行过程中,动态改变的:比如:是变量实例所存储的真是的值. ...

  5. Go语言基础之6--值类型和引用类型

    一. 引用类型 引用类型理解为(C语言):指针 Golang中只有三种引用类型:slice(切片).map(字典).channel(管道): 实例1-1 package main import &qu ...

  6. sql的几种常用锁简述

    比较全的文章地址保存下:http://www.cnblogs.com/knowledgesea/p/3714417.html SELECT * FROM dbo.AASELECT * FROM dbo ...

  7. =与==、&与&&、| 与 || 的区别

    =与== =属于赋值运算符,将右侧的值赋给左侧的变量名称 ==属于关系运算符,判断左右两边值是否相等,结果为boolean类型 &与&& &是逻辑与,&& ...

  8. maya安装不了

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  9. ubuntu 16.04安装后不能登入

    启动后,选择ubuntu高级选项,选择恢复模式,在恢复模式下 sudo apt-get update sudo apt-get upgrade 另外,可以在此模式下,选择nvidia驱动

  10. AngularJS directive 动态 template

    app.directive('testwindow', function() { return { restrict : 'E', template: '<ng-include src=&quo ...