Python基础学习-列表的常用方法
列表方法
=
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基础学习-列表的常用方法的更多相关文章
- Python基础学习 -- 列表与元组
本节学习目的: 掌握数据结构中的列表和元组 应用场景: 编程 = 算法 + 数据结构 数据结构: 通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合,这些元素可以是数字或者字符,或者其他数据 ...
- Python基础学习-列表基本操作
列表:Python的“苦力”. 列表不同于元组和字条串的地方:列表是可变的——可以改变列表的内容,并且列表有很多有用的.专门的方法. 1.list函数 因为字符串不能像列表一样被修改,所有有时根 ...
- Python基础学习----字符串的常用方法
# Python字符串 # 大多数的语言定义字符串是双引号,Python既可以双引号,也可以单引号.但使用也有区别 # 单双引号的使用 My_name="bai-boy" Demo ...
- Python基础学习----列表
name_list=["张无忌","张三丰","张小明","胡歌","夏东海"] #循环输出na ...
- Python入门基础学习(列表/元组/字典/集合)
Python基础学习笔记(二) 列表list---[ ](打了激素的数组,可以放入混合类型) list1 = [1,2,'请多指教',0.5] 公共的功能: len(list1) #/获取元素 lis ...
- Python基础与科学计算常用方法
Python基础与科学计算常用方法 本文使用的是Jupyter Notebook,Python3.你可以将代码直接复制到Jupyter Notebook中运行,以便更好的学习. 导入所需要的头文件 i ...
- Day1 Python基础学习
一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编写程序,本质仍然是直接操作 ...
- 0003.5-20180422-自动化第四章-python基础学习笔记--脚本
0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...
- Day1 Python基础学习——概述、基本数据类型、流程控制
一.Python基础学习 一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编 ...
随机推荐
- P1111 修复公路(并查集)
题目背景 AA地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 题目描述 给出A地区的村庄数NN,和公路数MM,公路是双向的.并告诉你每条公路的连着哪两个村庄,并告诉你 ...
- ORA-14517: Subpartition of index "string.string" is in unusable state
今天碰到个ORA-03113, 原因不明. 猜测因为某些table DDL操作过后导致index unuable的case, 然后进行analyze table, 再碰到ORA-14517. 最后通 ...
- poj2001 Shortest Prefixes(字典树)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21642 Accepted: 926 ...
- zabbix监控nginx mysql 服务添加
[root@test2 ~]# rpm -ivh nginx-1.8.0-1.el7.ngx.x86_64.rpm [root@test2 ~]# cd /etc/nginx/ [root@test2 ...
- python练习六十四:EXCEL文件处理
假设要读取number.txt文件中内容,code.txt文件内容如下 [ [1,2,3], [4,5,6], [7,8,9] ] 数据写入表格,如图 写文件(如果有文件,那直接调用就行,我这里自己先 ...
- Vue循环中多个input绑定指定v-model
Vue.js中提供了v-model可以双向绑定表单元素,这个方法可以非常方便的获得输入的值,但是有时候表单元素需要循环生成,在循环中要怎样获得指定输入框的值呢 这里介绍两种,一种是v-for中循环生成 ...
- protobuf在c++的使用方法以及在linux安装
https://blog.csdn.net/wangyin668/article/details/80046798 https://www.cnblogs.com/zhouyang209117/p ...
- cookie使用举例(添加购物车商品_移除购物车商品)
之前介绍过cookie和session的原理和区别.下面举例说明一下cookie在实际项目中的使用.使用cookie实现购物车功能: 1.往购物车添加商品 2.从购物车里移除商品 主要是要点是:以产品 ...
- numpy初用
import numpy as np for k,v in stat.iteritems(): print k v.sort() #v = v[len(v)*3/100:len ...
- js 基础学习笔记(一)
javascript基础 .组成部分:由 ECMAScript(翻译,核心,解释器).DOM(操作HTML的能力).BOM(浏览器window)三部分组成. 兼容性依次为 [1.几乎没有兼容性问题.2 ...