列表方法

=

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. 2016"百度之星" - 资格赛(Astar Round1) B

    Problem Description 度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个新的序列.对于给定的一个全1序列,请计算根据以上方法,可以构成多少种 ...

  2. [例] 用MappedByteBuffer更新文件内容

    import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; impor ...

  3. python 格式化时间含中文报错: 'locale' codec can't encode character '\u5e74'

    执行下面代码报错: UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal ...

  4. mc02_配置本地git仓库并上传到github

    注册github账号 仔细阅读使用说明便可,这里提一下如何删除一个repository. 点击要删除的repository,打开后点击Settings 然后滚动到页面最下方,点击最后一个按钮 在弹出框 ...

  5. hadoop单机配置

    条件: 先下载VMware1.2,然后安装. 下载ubuntu-1.4.05-desktop-amd64.iso.下载地址:http://mirrors.aliyun.com/ubuntu-relea ...

  6. vue首次赋值不触发watch(deep immediate handler)

    deep:默认值是 false,代表是否深度监听.immediate:true代表如果在 wacth 里声明了之后,就会立即先去执行里面的handler方法,如果为 false就跟我们以前的效果一样, ...

  7. python csv.reader参数指定

  8. PlayMaker 操作界面超级详细介绍

    原文:https://www.indienova.com/u/christiantam/blogread/1214

  9. LeetCode 454.四数相加 II(C++)

    给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单化,所有的 A ...

  10. #define\const\inline的区别与联系

    总结: const用于代替#define一个固定的值,inline用于代替#define一个函数.是#define的升级版,为了消除#define的缺陷. 参考内容:http://www.cnblog ...