標籤:切片 == play 一個 迭代 返回 use fas end
list增刪改查
1 # [1,2,‘c‘,‘dfas‘,True] 2 #索引和切片。 3 # li = [1,2,3,‘af‘,‘re‘,4,‘45‘] 4 # print(l[0]) 5 # print(l[3]) 6 # print(l[-1]) 7 #切片: 8 # print(li[0:3]) 9 # print(li[2:5])10 # print(li[0:5:2])11 # print(li[-2::-2])12 # print(li[5:0:-2])13 #苑昊14 # li = [‘taibai‘,‘alex‘,‘wusir‘]15 #1增加 append 在最後增加一個元素16 # print(li.append(‘yuanhao‘))17 # li.append([1,2,3,‘www‘])18 # print(li)19 # while True:20 # username = input(‘請輸入員工姓名:‘)21 # if username.lower() == ‘q‘:break22 # li.append(username)23 # print(li)24 #insert 插入25 # li = [‘taibai‘,‘alex‘,‘wusir‘]26 # li.insert(1,‘日天‘)27 # print(li)28 #extend 迭代的添加29 # li.extend(‘q‘)30 # li.extend(‘asdt‘)31 # li.extend([1,2,3,‘www‘])32 # print(li)33 li = [‘taibai‘,‘alex‘,‘wusir‘,‘egon‘,‘hulu‘,‘jingnvshen‘]34 #刪除 pop 按照索引去刪除,有傳回值35 name = li.pop(1)36 # print(li)37 # print(name)38 #remove 按照元素去刪除39 # li.remove(‘alex‘)40 # print(li)41 #clear 清空列表42 # li.clear()43 # print(li)44 #del45 # li = [‘taibai‘,‘alex‘,‘wusir‘,‘egon‘,‘hulu‘,‘jingnvshen‘]46 # del li[2:4]47 # print(li)48 # del li[0:4:2]49 # print(li)50 li = [‘taibai‘,‘alex‘,‘wusir‘,‘egon‘,‘hulu‘,‘jingnvshen‘]51 #改 切片先刪除,迭代著添加52 # li[0] = ‘男神‘53 # print(li)54 # li[0:3] = ‘都是男人‘55 # print(li)56 li[:] = ‘都是男人‘57 print(li)58 # li[0:2] = [‘asd‘]59 # print(li)60 # li[0:2] = [‘asd‘,‘fdsa‘,123]61 # print(li)62 #查63 # print(li[1:4])64 # for i in li:65 # print(i)66 # li = [‘taibai‘,‘taibai‘,‘wusir‘,‘egon‘,‘taibai‘,‘jingnvshen‘]67 # del li68 # print(li)
View Code列表的嵌套
1 li = [1,2,5,‘taibai‘,‘yuanhao‘,[1,‘alex‘,3,],True] 2 ‘‘‘ 3 # print(li[3]) 4 # print(li[3][3]) 5 # s = li[4].capitalize() 6 # li[4] = s 7 #li[4] = li[4].capitalize() 8 # li[4] = ‘Yuanhao‘ 9 # print(li)10 # s2 = li[4].replace(‘hao‘,‘日天‘)11 # li[4] = s212 # li[4] = li[4][0:4] + ‘ritian‘13 # print(li)14 # l2 = li[5]15 # li[5][0] = ‘文傑‘16 # print(li)17 ‘‘‘18 li[5][1] = li[5][1].upper()19 print(li)
View Code
列表的其他方法
1 # li = [1,2,3,1,‘a‘,1,‘4‘] 2 #count 計數 3 # print(li.count(‘fasd‘)) 4 li = [2,3,1,5,6,8,9,7,4,10] 5 #sort 正序排序 6 # li.sort() 7 # print(li) 8 #li.sort(reverse=True) 倒敘排序 9 # reverse 反轉10 # li.reverse()11 # print(li)
View Code
python增刪改查,列表