🍉CSDN小墨&晓末:https://blog.csdn.net/jd1813346972
个人介绍: 研一|统计学|干货分享
擅长Python、Matlab、R等主流编程软件
累计十余项国家级比赛奖项,参与研究经费10w、40w级横向
【Python操作基础】系列——列表操作,建议收藏!
该篇文章首先利用Python展示了使用列表类型数据的相关操作,包括基本用法;切片操作;反向遍历;类型转换;extend和append的区别;列表推导式;删除和插入;常用操作函数等。
1 基本用法
运行程序:
myList1 = [21,22,23,24,25,26,27,28,29] myList1
myList2=myList1 myList2
myList3=list(“Data”) myList3
myList1[-1] #倒数第几个
myList1[-9]
#myList1[9] #报错:超过边界
2 切片操作
运行程序:
myList1 = [21,22,23,24,25,26,27,28,29]
myList1
myList1[1:8] #第2个到第个
myList1[1:8:2] #步长为2
myList1[:5] #第1个到第5个
myList1[:] #所有
myList1[2:] #第3个到最后
myList1[:-1] #第1个到倒数第2个
运行结果:
[21, 22, 23, 24, 25, 26, 27, 28, 29]
[22, 23, 24, 25, 26, 27, 28]
[22, 24, 26, 28]
[21, 22, 23, 24, 25]
[21, 22, 23, 24, 25, 26, 27, 28, 29]
[23, 24, 25, 26, 27, 28, 29]
29
3 反向遍历
运行程序:
myList1 = [21,22,23,24,25,26,27,28,29]
myList1
myList1[::-1] #倒序
myList1
myList1[:-1]
reversed(myList1) #返回值为一个迭代器
list(reversed(myList1)) #list强制转换显示内容
myList1
myList1.reverse()#内置函数不改变列表本身
myList1
运行结果:
[21, 22, 23, 24, 25, 26, 27, 28, 29]
[29, 28, 27, 26, 25, 24, 23, 22, 21]
[21, 22, 23, 24, 25, 26, 27, 28, 29]
[21, 22, 23, 24, 25, 26, 27, 28]
<list_reverseiterator at 0x26c0cdc18d0>
[29, 28, 27, 26, 25, 24, 23, 22, 21]
[21, 22, 23, 24, 25, 26, 27, 28, 29]
[29, 28, 27, 26, 25, 24, 23, 22, 21]
4 类型转换
运行程序:
list("chaolemen")
运行结果:
['c', 'h', 'a', 'o', 'l', 'e', 'm', 'e', 'n']
5 extend与append的区别
运行程序:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all" ##执行多输出
myList1 = [21,22,23,24,25,26,27,28,29]
myList2=myList1
myList1 + myList2
myList1 = [21,22,23,24,25,26,27,28,29]
myList2=myList1
myList1.extend(myList2) #列表1上后面加列表2,以成员身份追加
myList1
myList1.append(myList2)#平等追加
myList1
myList1 = [1,2,3,4,5,6,7,8,9]
myList3 = [11,12,13,14,15,16,17,18,19]
[i + j for i, j in zip(myList1, myList3)] #两个列表数值相加
运行结果:
[21, 22, 23, 24, 25, 26, 27, 28, 29, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 21, 22, 23, 24, 25, 26, 27, 28, 29, [...]]
[12, 14, 16, 18, 20, 22, 24, 26, 28]
6 列表推导式
运行程序:
[2 for i in range(20)]
[i for i in range(1, 21)]
[i for i in range(1, 21, 2)]
range(10)
list(range(0,10,2))
[type(item) for item in [True,"1",1,1.0]]
print([ord(i) for i in ['朝', '乐', '门']])
["input/%d.txt" % i + "dd%d" % i for i in range(5)]#可以用字符串的占位符
["input/%d.txt"%i + "_%d" %i for i in range(5)]
运行结果:
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
range(0, 10)
[0, 2, 4, 6, 8]
[bool, str, int, float]
[26397, 20048, 38376]
['input/0.txtdd0',
'input/1.txtdd1',
'input/2.txtdd2',
'input/3.txtdd3',
'input/4.txtdd4']
['input/0.txt_0',
'input/1.txt_1',
'input/2.txt_2',
'input/3.txt_3',
'input/4.txt_4']
7 删除与插入
运行程序:
lst_1 = [10,10,11,12,13,14,15]
lst_1.insert(1, 8) #插入insert(插入下标位置, 插入值)
lst_1
lst_1 = [10,10,11,12,13,14,15]
lst_1.pop(2) #删除列表下标为2的值
lst_1
lst_1 = [10,10,11,12,13,14,15]
del lst_1[2] #下标删除
lst_1
lst_1 = [10,10,11,12,13,14,15]
lst_1.remove(10) #只删除第一个10
lst_1
lst_1 = [10,10,11,12,11,13,14,15]
for i in lst_1 :
if i == 11:
lst_1.remove(i) #删除11
print(lst_1)
lst_1 = [10,10,11,12,11,13,14,15]
[x for x in lst_1 if x != 10] #删除所有10
lst_1 = [10,10,11,12,11,13,14,15]
list(filter(lambda i:i!=10, lst_1)) #过滤10
lst_1 = [10,10,11,12,11,13,14,15]
list(set(lst_1))#过滤重复值
运行结果:
[10, 8, 10, 11, 12, 13, 14, 15]
11
[10, 10, 12, 13, 14, 15]
[10, 10, 12, 13, 14, 15]
[10, 11, 12, 13, 14, 15]
[10, 10, 12, 13, 14, 15]
[11, 12, 11, 13, 14, 15]
[11, 12, 11, 13, 14, 15]
[10, 11, 12, 13, 14, 15]
8 常用操作函数
运行程序:
lst_1 = [10,10,11,12,13,14,15]
len(lst_1) #列表长度
lst_1 = [10,10,11,12,11,13,14,15]
sorted(lst_1) #列表排序,正序,不改变原序列
lst_1
lst_1 = [10,10,11,12,11,13,14,15]
lst_1.sort() #列表排序,正序,改变原序列
lst_1
lst_1 = [10,10,11,12,11,13,14,15]
lst_2=[11,12,13,14] #1列表后加2列表,加一整个列表
lst_1.append(lst_2)
print(lst_1)
lst_1 = [10,10,11,12,11,13,14,15]
lst_2=[11,12,13,14]
lst_1.extend(lst_2) # #1列表后加2列表,直接原列表后面加
print(lst_1)
lst_1 = [1,2,3,'Python',True,4.3,None]
lst_2 = [1,2,[2,3]]
print(lst_1, lst_2) #依次输出两个列表
lst_1 = [1,2,3,'Python',True,4.3,None]
list(reversed(lst_1)) #倒序列表
reversed(lst_1) #倒序列表,输出格式
lst_1
[1, 2, 3, 'Python', True, 4.3, None]
lst_1 = [1,2,3,'Python',True,4.3,None]
lst_1.reverse()#倒序列表
lst_1
str1=[1,2,3,4,5]
str2=[20,21,23,24,25]
print(zip(str1,str2))#两列表同步运算,输出格式
print(list(zip(str1,str2))) #强制转化为值
str1=["a","about","c","china","b","beijing"]
[x.upper() for x in str1 if len(x)>1] #输出列表中长度大于1的字符串
[x**2 for x in range(10)] #列表运算
str1=["a","about","c","china","b","beijing"]
[str2.upper() for str2 in str1 if len(str2)>1]#输出列表1中长度大于1的字符串
myList=[2,3,5,6,7,3,2]
list(enumerate(myList))#内置函数
i=0
sum=0
for value in myList:
i=i+1
sum=value+i
sum
sum=0
[dict((value,i) for i, value in enumerate(myList))]
运行结果:
7
[10, 10, 11, 11, 12, 13, 14, 15]
[10, 10, 11, 12, 11, 13, 14, 15]
[10, 10, 11, 11, 12, 13, 14, 15]
[10, 10, 11, 12, 11, 13, 14, 15, [11, 12, 13, 14]]
[10, 10, 11, 12, 11, 13, 14, 15, 11, 12, 13, 14]
[1, 2, 3, 'Python', True, 4.3, None] [1, 2, [2, 3]]
[None, 4.3, True, 'Python', 3, 2, 1]
<list_reverseiterator at 0x2cd44ac2f28>
[1, 2, 3, 'Python', True, 4.3, None]
[1, 2, 3, 'Python', True, 4.3, None]
[None, 4.3, True, 'Python', 3, 2, 1]
<zip object at 0x000002CD42AA2E48>
[(1, 20), (2, 21), (3, 23), (4, 24), (5, 25)]
['ABOUT', 'CHINA', 'BEIJING']
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
['ABOUT', 'CHINA', 'BEIJING']
[(0, 2), (1, 3), (2, 5), (3, 6), (4, 7), (5, 3), (6, 2)]
9
[{2: 6, 3: 5, 5: 2, 6: 3, 7: 4}]