python_20_列表

#1
names=["QiZhiguang","DaiYang","HuZhongtao","ZhangDong"]
print(names)
print(names[0])#print(names["QiZhiguang"])是错误的
print(names[0],names[2])
#2 切片(自左向右)      注:顾头不顾尾(左闭右开)
print(names[1:2])#取得是DaiYang
print(names[1:3])#取得是DaiYang,HuZhongtao
print(names[0:3])#取得是前三个0 1 2位置
print(names[:3])#与上试效果相同
print(names[3])#取得是最后一个
print(names[-1])#取得是最后一个
print(names[-2])#取得是倒数第二个
print(names[-2:])#取得是最后两个值(自左向右),所以print(names[-1:-3])是错得
#3 追加
names.append("PengDexue")#放在了最后一位
print(names)
#3 插入(无批量插入,只能一个一个来)
names.insert(1,"LinFang")#放在了第二个位置
print(names)
#3 修改替换,例把QiZhiguang改成WenXuehan
names[0]="WenXuehan"
print(names)
#4 删除
names.remove("WenXuehan")#也可以通过 del names[1]
names.pop()#默认删除最后一个,如果输入下标,例names.pop(1)=del names[1]
print(names)
#5寻找某人位置
print(names.index("HuZhongtao"))
print(names[names.index("HuZhongtao")])
#6统计相同元素
names1=["QiZhiguang","DaiYang","HuZhongtao","QiZhiguang","ZhangDong"]
print(names1.count("QiZhiguang"))
#7反转列表
names1.reverse()
print(names1)
#8排序列表(按照ASCALL码排序的)
names1.sort()
print(names1)
#9扩展列表
names.extend(names1)
print(names)
print(names,names1)#两个列表依然存在
#10浅copy
names3=["QiZhiguang","DaiYang","HuZhongtao",["YaoLinjin","LiZikang"],"QiZhiguang","ZhangDong"]
names4=names3.copy()#浅层copy,列表处复制的是地址(通过指针指向相应的内容)
names3[2]="齐志光"
names3[3][0]="姚林静"
print(names3)
print(names4)
names4[3][1]="李子康"
print(names3)
print(names4)
#11深copy
import copy
names5=["Q","D","H",["Y","L"],"Q","Z"]
names6=copy.deepcopy(names5)#深copy
names7=copy.copy(names5)#浅copy
names5[2]="齐"
names5[3][0]="姚"
print(names5)
print(names6)
print(names7)
#12 for循环
for i in names6:
    print(i)
#13 切片
names1=["QiZhiguang","DaiYang","HuZhongtao","QiZhiguang","ZhangDong"]
print(names1[0:-1:2])#从第一个切到最后一个(不包括最后一个),间隔为1个
print(names1[::2])#从第一个切到最后一个(包括最后一个),间隔为1个
print(names1[:])#完全切片
#11清除列表
names1.clear()
print(names1)
#12删除列表
del names
#print(names)#出错,因为已经删除names
#二维列表
list=[['cpu','flash'],['usb','主机']]
for i in range(len(list)):
    print(list[i])
print(">>>>>>>>>>>>>>>>>>>>>>>>")
for i in range(len(list)):
    list1=list[i]
    for j in range(len(list1)):
        print(list1[j])
print(">>>>>>>>>>>>>>>>>>>>>>>>")
for i in range(len(list)):
    for j in range(len(list[i])):
        print(list[i][j])

补充:

aList=[1,3,5,7,9,2,5,4,6,8,10]
print(aList.index(5))#2
print(aList.index(5,4,10))#6

 

 

 

posted @ 2018-01-03 09:29  耐烦不急  阅读(207)  评论(0编辑  收藏  举报