python之列表(list)
阿新 • • 發佈:2018-04-14
list python 列表
#列表
names = ["Zhangwuji","Zhaomin","Yangguo","Xiaolongnv","Guojing","Huangrong"]
# print(names)
# print(type(names))
#切片
# print(names[1]) #在列表中取出zhaomin,列表中下村從0開始,[0,1,2,3,4,5.....]
#切片中顧頭不顧尾
# print(names[:2]) #取當前列表中第一個值,第二個值
# print(names[2:4]) #取當前列表中yangguo,xiaolongnv,
#切片中自右而左數,默認是從-1開始
# print(names[-1]) #取當前列表中最後一個值
# print(names[-2:]) #取當前列表中倒數兩個值
# print(names.index("Zhaomin")) #通過值來查找其對應的下標
# names.append("Huanglaoxie") #按順序追加到當前列表中
# names.insert(1,"Zhouzhiruo") #在指定的位置插入數據
# names[1]="Hahaha" #改變列表中的值
#刪除列表中的值
# names.remove("Zhaomin") #刪除指定的值
# del names[1] #刪除指定下標位置的值
# names.pop(1) # 刪除指定的下標位置的值,如果不加下標,默認刪除最後一個值
# names.clear() #清空列表
# print(names.count("Zhaomin")) #統計某個值出現的次數
# names.reverse() #反轉列表
name2 = [1,2,3,4,5]
# names.extend(name2) #合並列表
print(name2)
#淺copy # names = ["Zhangwuji","Zhaomin",["tom","jack"],"Yangguo","Xiaolongnv","Guojing","Huangrong"] # name2 = names.copy() # names[2][0] = "TOM" # names[1] = "趙敏" # print(names) # print(name2) #深copy # import copy # names = ["Zhangwuji","Zhaomin",["tom","jack"],"Yangguo","Xiaolongnv","Guojing","Huangrong"] # name2 = copy.deepcopy(names) # names[2][0] = "TOM" # names[1] = "趙敏" # print(names) # print(name2) #跳著切片 # a = [1,2,3,4,5,6] # print(a[0:-1:2]) # print(a[::2])
python之列表(list)