栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python list

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python list

同属于一个列表的数据,可以是不同的类型
特色:存储于用一个列表的数据都是以数字来作为索引的,即作为操作存取其中各个元素的依据。
a_list
0 1 2 3 4
int int int int int
1 3 5 7 9
索引分别为 0,1,2,3,4
每个元素可有自已的类型,均为int,内容分别是
1、3、5、7、9
a_list = [ 1,3,5,7,9 ]
数字列表
>>> a_list=[1,3,5,7,9]
>>> a_list
[1, 3, 5, 7, 9]
>>> a_list[0]
1
字符串列表
>>> str_list=['P','y','t','h','o','n']
>>> str_list
['P', 'y', 't', 'h', 'o', 'n']
>>> str_list[2]
't'
字符串split 方法
>>> str_msg="I Love Pyton"
>>> b_list=str_msg.split()
>>> b_list
['I', 'Love', 'Pyton']
一个英文句子拆成字母所组成的列表,用list() 函数,
>>> str_msg="I Love Pyton"
>>> c_list=list(str_msg)
>>> c_list
['I', ' ', 'L', 'o', 'v', 'e', ' ', 'P', 'y', 't', 'o', 'n']
>>>
同一个列表中可以用不同的数据类型,列表中也可以有其他的列表
>>> k1=['book',10]
>>> k2=['campus',15]
>>> k3=['cook',9]
>>> k4=['Python',26]
>>> keywords=[k1,k2,k3,k4]
>>> keywords
[['book', 10], ['campus', 15], ['cook', 9], ['Python', 26]]
>>> keywords[2]
['cook', 9]
>>> keywords[2][0]
'cook'
>>> keywords[2][1]
9
>>>
可以使用”+“运算把两个列表放在一起,还可以 检测某一个数据是否在列表之中
>>> "Python" in k4
True
>>> k4 in keywords
True
>>> ["Python",26] in keywords
True
>>> keywords+k1+k2
[['book', 10], ['campus', 15], ['cook', 9], ['Python', 26], 'book', 10, 'campus', 15]
>>>

比较常用的操作方法和函数

列表表达式                                         操作结果说明
lst * n     把lst类表重复n次
lst[n1:n2]      把索引组n1到n2的列表内容取出,组成一个列表
lst[n1:n2:k]    同上,但取出间隔为k
del lst[n1:n2]  删除索引值n1到n2之间的元素
lst[n1:n2]=n    把索引值n1到n2之间的元素设置为n
lst[n1:n2:k]=n  同上,但间隔为k
del lst[n1:n2:k]    删除索引值n1到n2之间的元素,但间隔为k
len(lst)    放回类表的个数
min(lst)    返回列表的最小值
max(lst)    返回列表的最大值
sum(lst)    返回列表的求和值
lst.index(n)    返回列表中第一个出现n的索引值
lst.count(n)    计算出n 在列表中出现的次数

>>> x=list(range(10))
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x*2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> y=x[1:7]
>>> y
[1, 2, 3, 4, 5, 6]
>>> y=x[1:7:2]
>>> y
[1, 3, 5]
>>> del x[1:7]
>>> x
[0, 7, 8, 9]
>>> x=list(range(10))
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del x[1:7:2]
>>> x
[0, 2, 4, 6, 7, 8, 9]
>>> x=list(range(10))
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> len(x)
10
>>> max(x)
9
>>> min(x)
0
>>> x=list(range(3))
>>> x
[0, 1, 2]
>>> sum(x)
3
>>> msg="Here is the string"
>>> lst=list(msg)
>>> lst
['H', 'e', 'r', 'e', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g']
>>> lst.index('e')
1
>>> lst.count('e')
3
>>>

列表操作方法:
(其中lst表示列表变量,x表示列表元素或是另外一个列表变量,n为数值)
lst.append(x)       将x视为一个元素,附加到列表的后面
lst.extend(x)       将x 中的所有元素附加到列表的后面
lst.insert(n,x)         把x插入到索引值为n的地方
lst.pop()               弹出列表中最后一个元素,可以参数指定特定的索引
lst.remove(x)       从列表中删除第一个出现的x
lst.reverse()       反转列表的顺序
lst.sort()          将列表的元素内容加以排序

append 与 extend 区别
>>> lsta=[1,2,3,4,5]
>>> exb=[5,5,5,5]
>>> lsta.extend(exb)
>>> lsta
[1, 2, 3, 4, 5, 5, 5, 5, 5]
>>> lsta=[1,2,3,4,5]
>>> lsta.append(exb)
>>> lsta
[1, 2, 3, 4, 5, [5, 5, 5, 5]]
pop操作
>>> lst=[0,1,2,3,4,5]
>>> lst.append(9)
>>> lst.append('x')
>>> lst
[0, 1, 2, 3, 4, 5, 9, 'x']
>>> lst.pop()
'x'
>>> lst.pop(2)
2
>>> lst
[0, 1, 3, 4, 5, 9]
>>>
>>> cars=['bwm','audi','toyota','subaru']
>>> cars.sort()
>>> cars
['audi', 'bwm', 'subaru', 'toyota']
>>> cars.reverse()
>>> cars
['toyota', 'subaru', 'bwm', 'audi']
>>>
>>> cars.remove('bwm')         用于不知道具体的索引位置,只知道具体的值
>>> cars
['toyota', 'subaru', 'audi']
>>> cars.insert(1,'haima')        指定位置插入    
>>> cars
['toyota', 'haima', 'subaru', 'audi']
>>>

>>> cars
['toyota', 'haima', 'subaru', 'audi']
>>> for x in cars:
...   print x
...
toyota
haima
subaru
audi
>>> for x in cars:
...    print x.title()
...
Toyota
Haima
Subaru
Audi
>>>
通过循环创建所需列表
>>> atest=[]
>>> for value in range(1,11,2):
...     a=value**2
...     atest.append(a)
...
>>> print atest
[1, 9, 25, 49, 81]

>>> atest2=[value**2 for value in range(1,11,2)]
>>> print atest2
[1, 9, 25, 49, 81]
>>>

>>> list_2d=[[0 for i in range(5)]for i in range(5)]
>>> list_2d
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
>>> list_2d[0].append(3)
>>> list_2d[2].append(7)
>>> list_2d
[[0, 0, 0, 0, 0, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 7], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
>>>

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/224444.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号