python列表、元組

來源:互聯網
上載者:User

標籤:完全   index   迭代   copy   fas   python   eve   列表   style   

 1 #Author:CGQ 2 import copy 3 #列表 4 ‘‘‘ 5 names=["ZhangYang","XiaoHei","XiaoHei","LiSan"] 6 print(names[0])#某一個 7 #切片 8 print(names[1:3]) 9 print(names[0:-1:2])#2代表步長10 print(names[-1])11 print(names[-2:])12 13 print(names[names.index("ZhangYang")])#輸出原文和位置資訊14 names.append("DaKaiZi")#追加最後15 names.insert(1,"GuYun")#追加到某位置16 names.extend("GuYun")#迭代添加17 names[2] = "XieDi"#替換18 names[:2] = "ni"19 names[:2] = "ni43243"#迭代替換20 names[:2] = [4,3,2,4,3]#迭代替換21 #刪除22 names.remove("XieDi")#按元素刪23 del names[2]#按序號刪24 del names#刪除names25 names.pop(2)#按序號刪26 n1 = names.pop(2)#刪除並返回刪除值(預設刪除最後一個)27 names.clear()#清空28 names.reverse()#反轉29 names.sort()#排序(字母順序,符號》數字》大寫》小寫)30 names.sort(reverse=True)#反向排序(字母順序,符號》數字》大寫》小寫)31 names2=[1,2,3,4]32 names.extend(names2)#連結33 del names2#刪變數34 ‘‘‘35 ‘‘‘36 names=["ZhangYang","XiaoHei",["CGQ","DaWang"],"XiaoHei","LiSan"]37 #複製第一層38 #names2=names.copy()39 #names2=names[:]40 #names2=list(names)41 #names2=names#指標指向name2,沒有建立(針對列表,字元、字串是建立【真正的複製】)42 names2=copy.deepcopy(names)#完全複製43 names[2][0]="cgq"44 #print(names,names2)45 for i in names:46     print(i)47 ‘‘‘48 #元組49 # name=("LiSi","WangWu")#不能修改50 # print(name.index("LiSi"))51 52 NAME = (1,2,[1,‘SDFAS‘],‘jgjhgj‘)#其中列表可以修改53 # NAME[2][1] = NAME[2][1].lower()54 # print(NAME)55 56 # s = ‘safsadf‘57 # s1 = ‘_‘.join(s)#用‘_‘相串連,返回字串58 # print(s1)59 #range   [......1,2,3........]60 # for i in range(-20,20,2):61 #     print(i)

 

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.