python学习笔记—列表相关

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习笔记—列表相关相关的知识,希望对你有一定的参考价值。

python中的列表等同于其他编程语言中的数组

基本操作有:

1.插入,追加,修改,删除

name = ["Type99","M1A2","T-72","Leclerc"]
print (name[2])
print (name[0:2])
print (name[1:-1])
print (name)
name.insert(2,"Type96") #插入元素
print (name)
name.append("Type59")  #追加元素
print (name)
name[-1] = "SuperType59" #修改元素
print (name)
name.remove("T-72")#删除列表中的元素
print (name)
#del name[0:3] #删除内存中的数据
#print (name)
#del name      #直接从内存中删除了整个变量
#print (name)

2.步长:

print (name[0::2])#最后一个冒号后面的数值是步长

3.对列表内的某个元素的判断:

name =
[759,456,116,15,1,161,4896,16,7596,51,60,6,1,65161,1,61,1,6,1,6,4,1,96,4,1,418,1,9633,1]
#print (15 in name) #判断15是否处于列表name
if 1 in name:
    num_of_ele = name.count(1)
    print("[%s]15 is in name" %num_of_ele) #判断有多少个1在这个列表中

4.判断有多少各1在列表中,第一个1处在什么位置:

name = [759,456,116,15,1,161,4896,16,7596,51,60,6,1,65161,1,61,1,6,1,6,4,1,96,4,1,418,1,9633,1]
if 1 in name:
    num_of_ele = name.count(1)
    position_of_ele = name.index(1)
    print ("[%s] 1 is in name,position of this number:%s" %(num_of_ele,position_of_ele))

5.判断列表中有多少个1,并将所有的1替换成Gooooooool.

name = [759,456,116,15,1,161,4896,16,7596,51,60,6,1,65161,1,61,1,6,1,6,4,1,96,4,1,418,1,9633,1]
for i in range(name.count(1)):
    position_of_ele = name.index(1)
    name[position_of_ele] = "Gooooooooooool"
print(name)

6.合并两个列表及倒序输出列表

name = [759,456,116,15,1,161,4896,16,7596,51,60,6,1,65161,1,61,1,6,1,6,4,1,96,4,1,418,1,9633,1]
name2 =["yc","L","X","Y","H"]
name.extend(name2)#追加列表
print(name)
print(name2)#追加不会删除原有列表
name.reverse()#倒序输出列表
name2.reverse()
print (name)
print (name2)

7.列表的复制:

name = ["Type99",["M1A2","T-90","T-72","T-34","Tiger","M4"],"Leclerc","Type62"]
name2 = name.copy()
print(name)
print(name2)
name[2] = "Type59"
print (name)
print (name2)
name[1][1] = "T-14"
print (name)
print (name2)
import copy
name3 = copy.copy(name)
name[1][2] = "M-26"
print(name)
print(name3)
name4 =copy.deepcopy(name)
name[1][3] = "KV-2"
print(name)
print(name4)
name5 =copy.deepcopy(name)
name[3] = "Firefly"
print(name)
print(name5)

输出如下:

[‘Type99‘, [‘M1A2‘, ‘T-90‘, ‘T-72‘, ‘T-34‘, ‘Tiger‘, ‘M4‘], ‘Leclerc‘, ‘Type62‘]

[‘Type99‘, [‘M1A2‘, ‘T-90‘, ‘T-72‘, ‘T-34‘, ‘Tiger‘, ‘M4‘], ‘Leclerc‘, ‘Type62‘]

[‘Type99‘, [‘M1A2‘, ‘T-90‘, ‘T-72‘, ‘T-34‘, ‘Tiger‘, ‘M4‘], ‘Type59‘, ‘Type62‘]

[‘Type99‘, [‘M1A2‘, ‘T-90‘, ‘T-72‘, ‘T-34‘, ‘Tiger‘, ‘M4‘], ‘Leclerc‘, ‘Type62‘]

[‘Type99‘, [‘M1A2‘, ‘T-14‘, ‘T-72‘, ‘T-34‘, ‘Tiger‘, ‘M4‘], ‘Type59‘, ‘Type62‘]

[‘Type99‘, [‘M1A2‘, ‘T-14‘, ‘T-72‘, ‘T-34‘, ‘Tiger‘, ‘M4‘], ‘Leclerc‘, ‘Type62‘]

[‘Type99‘, [‘M1A2‘, ‘T-14‘, ‘M-26‘, ‘T-34‘, ‘Tiger‘, ‘M4‘], ‘Type59‘, ‘Type62‘]

[‘Type99‘, [‘M1A2‘, ‘T-14‘, ‘M-26‘, ‘T-34‘, ‘Tiger‘, ‘M4‘], ‘Type59‘, ‘Type62‘]

[‘Type99‘, [‘M1A2‘, ‘T-14‘, ‘M-26‘, ‘KV-2‘, ‘Tiger‘, ‘M4‘], ‘Type59‘, ‘Type62‘]

[‘Type99‘, [‘M1A2‘, ‘T-14‘, ‘M-26‘, ‘T-34‘, ‘Tiger‘, ‘M4‘], ‘Type59‘, ‘Type62‘]

[‘Type99‘, [‘M1A2‘, ‘T-14‘, ‘M-26‘, ‘KV-2‘, ‘Tiger‘, ‘M4‘], ‘Type59‘, ‘Firefly‘]

[‘Type99‘, [‘M1A2‘, ‘T-14‘, ‘M-26‘, ‘KV-2‘, ‘Tiger‘, ‘M4‘], ‘Type59‘, ‘Type62‘]

注:

1.copy.deepcopy()是标准库copy提供的,所以在使用之前需要先申明

2.name.copy所进行的copy类似于软链接形式,在上述例子中嵌套列表首先两个列表在内存中都拥有内存空间,copy之后,外部的列表重新开辟了一块新的内存空间以供自己使用,内部嵌套的列表还是使用原本的内存空间。

技术分享

copy.deepcopy()就是直接copy一个完整的列表,原来有多少内存空间,就新开辟几个内存空间。

 

第一次写,有任何不当,欢迎指正

 

以上是关于python学习笔记—列表相关的主要内容,如果未能解决你的问题,请参考以下文章

python学习笔记第八节(函数的相关作业)

python学习笔记二--列表

Python-列表学习笔记(完)

python爬虫学习笔记-M3U8流视频数据爬虫

python学习笔记012——pdb调试

python学习笔记第二天