列表

列表的使用

从0开始计算,names[0] = "ZhangSan"     

names = ["ZhangSan","LiSi",["ZhaoQian,SunSan"],"WangWu"]

 特殊取值:顾头不顾尾name

names = ["ZhangSan","LiSi",["ZhaoQian,SunSan"],"WangWu"]
print(names[1:3])

结果:
['LiSi', ['ZhaoQian,SunSan']]

切片

names = ["ZhangSan","LiSi",["ZhaoQian,SunSan"],"WangWu"]
print(names[-1])#取最后一个
print(names[-2])#取倒数第二个

print(names[-2:])#取最后两个
 

 

  • 增append()
names.append("ZhaoLiu") #在列表最后增加

  

  • insert():在列表的第四个位置,插入

 

names = ["ZhangSan","LiSi",["ZhaoQian,SunSan"],"WangWu"]
names.insert(3,"赵谦")
print(names)

#结果
['ZhangSan', 'LiSi', ['ZhaoQian,SunSan'], '赵谦', 'WangWu']
  •  删除
names.remove("LiSi")#删除指定值
names.pop()#删除最后一个
names.pop(1)#删除第一个位置的内容
del names[1]#删除第一个位置的内容 ==names.pop(1)

del names #删除整个列表

 

  

 

  • 查index()

 

names = ["ZhangSan","LiSi",["ZhaoQian,SunSan"],"WangWu"]
print(names.index("LiSi"))#查找LiSi所在位置
print(names[names.index("LiSi")])#根据查找所在位置后,获取LiSi
  • count()
names = ["ZhangSan","LiSi",["ZhaoQian,SunSan"],"WangWu","LiSi"]
print(names.count('LiSi'))#查找列表中Lisi的数量

 

  • clear()  清空列表内容
  • reverse()   反转:将列表顺序变成从后到开始打印
names = ["ZhangSan","LiSi",["ZhaoQian,SunSan"],"WangWu","LiSi"]
print("反转前;",names)
names.reverse()
print("反转后:",names)

结果:
反转前; ['ZhangSan', 'LiSi', ['ZhaoQian,SunSan'], 'WangWu', 'LiSi']
反转后: ['LiSi', 'WangWu', ['ZhaoQian,SunSan'], 'LiSi', 'ZhangSan']

 

  • sort()   排序:注意若列表里有列表,则不能使用
names = ["4ZhangSan","#LiSi","WangWu","LiSi"]
print("排序前;",names)
names.sort()
print("排序后:",names)

排序前; ['4ZhangSan', '#LiSi', 'WangWu', 'LiSi']
排序后: ['#LiSi', '4ZhangSan', 'LiSi', 'WangWu']

 

  • extend() 合并:将两个列表合并
names = ["4ZhangSan","#LiSi","WangWu","LiSi"]
names2 = [1,2,3,4,]
names.extend(names2)
print(names)
print(names2)

结果:
['4ZhangSan', '#LiSi', 'WangWu', 'LiSi', 1, 2, 3, 4]
[1, 2, 3, 4]

 

  • copy()   浅复制
names = ["ZhangSan","LiSi","WangWu","LiSi"]
names2 = names.copy()
print(names)
print(names2)
names2[2] = "李四"
print(names)
print(names2)


结果:
['ZhangSan', 'LiSi', 'WangWu', 'LiSi']
['ZhangSan', 'LiSi', 'WangWu', 'LiSi']
['ZhangSan', 'LiSi', 'WangWu', 'LiSi']
['ZhangSan', 'LiSi', '李四', 'LiSi']

若列表包含列表的情况

names = ["ZhangSan","LiSi","WangWu",["alex","he","Hello"],"LiSi"]
names2 = names.copy()
print(names)
print(names2)
names2[3][1] = "李四"
print(names)
print(names2)

结果:
['ZhangSan', 'LiSi', 'WangWu', ['alex', 'he', 'Hello'], 'LiSi']
['ZhangSan', 'LiSi', 'WangWu', ['alex', 'he', 'Hello'], 'LiSi']
['ZhangSan', 'LiSi', 'WangWu', ['alex', '李四', 'Hello'], 'LiSi']
['ZhangSan', 'LiSi', 'WangWu', ['alex', '李四', 'Hello'], 'LiSi']
  • import copy

导入copy里面的copy方法

import copy
names = ["ZhangSan","LiSi","WangWu",["alex","he","Hello"],"LiSi"]
names2 = copy.copy(names)
print(names)
print(names2)
names2[3][1] = "李四"
print(names)
print(names2)

结果:
['ZhangSan', 'LiSi', 'WangWu', ['alex', 'he', 'Hello'], 'LiSi']
['ZhangSan', 'LiSi', 'WangWu', ['alex', 'he', 'Hello'], 'LiSi']
['ZhangSan', 'LiSi', 'WangWu', ['alex', '李四', 'Hello'], 'LiSi']
['ZhangSan', 'LiSi', 'WangWu', ['alex', '李四', 'Hello'], 'LiSi']

导入copy里面的deepcopy()方法(这个尽量不使用)

import copy
names = ["ZhangSan","LiSi","WangWu",["alex","he","Hello"],"LiSi"]
names2 = copy.deepcopy(names)
print(names)
print(names2)
names2[3][1] = "李四"
print(names)
print(names2)

结果:
['ZhangSan', 'LiSi', 'WangWu', ['alex', 'he', 'Hello'], 'LiSi']
['ZhangSan', 'LiSi', 'WangWu', ['alex', 'he', 'Hello'], 'LiSi']
['ZhangSan', 'LiSi', 'WangWu', ['alex', 'he', 'Hello'], 'LiSi']
['ZhangSan', 'LiSi', 'WangWu', ['alex', '李四', 'Hello'], 'LiSi']

 

  • 列表在循环打印

 

names = ["ZhangSan","LiSi","WangWu",["alex","he","Hello"],"LiSi"]
for i in names:#可以省略为for i in names[:]   或者 for i in names[::]
    print(i)


结果:
ZhangSan
LiSi
WangWu
['alex', 'he', 'Hello']
LiSi

 

  •  元祖:

元祖是一个只读列表,只能查询,不能进行增、删、改

写法:names = ("Alex","ZhangSan","LiSi")

  • 列表的切片

 

names = ["ZhangSan","LiSi","WangWu",["alex","he","Hello"],"LiSi"]
#类似 range(0,10,2)这样进行切片,从头到尾,间隔1个打印
for i in names[0:-1:2]:#可以省略为for i in names[::2]:
    print(i)


结果:
ZhangSan
WangWu
posted @ 2017-11-28 22:43  雨之愿风  阅读(133)  评论(0编辑  收藏  举报