python增刪改查,列表

來源:互聯網
上載者:User

標籤:切片   ==   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增刪改查,列表

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.