Python的序列,Python序列

來源:互聯網
上載者:User

Python的序列,Python序列
Python包含6種內建的序列:列表,元組,字串,Unicode,buffer對象,xrange對象。

通用序列操作包括:索引,分區,加,乘,成員資格。

另外還有長度(len),最小值(min),最大值(mix)內建函數

  • 列表 

基本的列表操作

#建立一個列表>>> nums = [1,2,3,5]>>> nums[1, 2, 3, 5]#元素賦值>>> nums[2] = 7>>> nums[1, 2, 7, 5]>>> #刪除元素>>> del nums[0]>>> nums[2, 7, 5]#list函數(類型)可以根據字串建立列表>>> st = list('Hello')>>> st['H', 'e', 'l', 'l', 'o']#分區 使用索引訪問單個元素,使用分區來訪問一定範圍的元素>>> nums = [1,2,3,4,5,6,7,8,9,10]>>> nums[2:6][3, 4, 5, 6]##為了讓分區能夠包含列表的最後一個元素,必須提供 大於最後一個元素索引的索引作為邊界>>> nums[2:10][3, 4, 5, 6, 7, 8, 9, 10]##從結尾開始計數>>> nums[-3:][8, 9, 10]##從開始開始計數>>> nums[:3][1, 2, 3]##整個列表>>> nums[:][1, 2, 3, 4, 5, 6, 7, 8, 9, 10]##步長>>> nums[0:10:3][1, 4, 7, 10]>>> nums[::4][1, 5, 9]>>> nums[::-2][10, 8, 6, 4, 2]##分區賦值>>> name = list('hemin')>>> name['h', 'e', 'm', 'i', 'n']>>> name[2:] = list('shen')>>> name['h', 'e', 's', 'h', 'e', 'n']##插入與刪除>>> nums2 = [1,5]>>> nums2[1:1] = [2,3,4]>>> nums2[1, 2, 3, 4, 5]>>> nums2[1:4] = []>>> nums2[1, 5]

列表方法

# append() 追加,append直接修改原來的列表,不是返回修改過的新列表>>> letters = ['a','b',"c"]>>> letters.append('d')>>> letters['a', 'b', 'c', 'd']#count() 統計指定元素出現的次數letters = list('aabcdeeefggggh')>>> letters.count('g')4#extend() 列表末尾追加多個值,或說可以用新列表擴充原列表>>> letters2 = list('ijklmn')>>> letters.extend(letters2)>>> letters['a', 'a', 'b', 'c', 'd', 'e', 'e', 'e', 'f', 'g', 'g', 'g', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']#註:extend 和 append 直接修改原來的列表,而串連操作會返回一個全新的列表,串連操作比extend方法效率低>>> a = [1,2,3]>>> b = [4,5,6]>>> a+b[1, 2, 3, 4, 5, 6]>>> a[1, 2, 3]#a沒改變#insert() 插入>>> nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> nums.insert(2,'two')>>> nums[1, 2, 'two', 3, 4, 5, 6, 7, 8, 9, 10]#pop pop方法會移除列表中的一個元素(根據索引移除)>>> nums.pop()#預設移除最後一個元素10>>> nums[1, 2, 'two', 3, 4, 5, 6, 7, 8, 9]>>> nums.pop(3)3>>> nums[1, 2, 'two', 4, 5, 6, 7, 8, 9]# remove() remove 方法會移除列表中的一個元素第一個匹配項(根據元素移除)>>> letters = list('abccd')>>> letters.remove('c')>>> letters['a', 'b', 'c', 'd']# reverse() 列表元素反向存放>>> le = list('abc')>>> le.reverse()>>> le['c', 'b', 'a']>>> #列表複製>>> x = [5,4,7]>>> y = x      #這樣x和y指向同一個列表>>> z = x[:]  #應該這樣複製>>> x.sort()>>> y[4, 5, 7]>>> z[5, 4, 7]#排序#sort() 列表排序>>> num = [5,7,6,9,2,1]>>> num.sort()>>> num[1, 2, 5, 6, 7, 9]#sort(*, key=None, reverse=None),key表示排序的關鍵字,reserve表示是否逆序。Python3的sort是穩定排序。>>>a=['abc','abcd','ab']>>> a.sort(key=len)>>> a['ab', 'abc', 'abcd']#clear() 清空列表#copy() 複製列表
  • 元組
#元組不能被修改#建立元組>>> 1,2,3(1, 2, 3)>>> 'a','b','c'('a', 'b', 'c')>>> ()#空元組()>>> ('d',) #只包含一個元素('d',)#元組的意義:元組可以在映射中當做鍵使用,列表不行;元組作為很多內建函數的方法和傳回值存在

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.