Python:列表函數

來源:互聯網
上載者:User

list函數:

將字串轉化成列表,例:

>>> name = list('hello')>>> name['h', 'e', 'l', 'l', 'o']
 
 
 
 
 
 
 


列表基本函數:

1、改變列表:元素賦值

使用索引標記

>>>x = [1, 1, 1]>>>x[1] = 2>>>x[1, 2, 1]


2、刪除元素

del語句實現

>>>names = ['one', 'two', 'three']>>>del names[1]>>>names['one', 'three']


3、分區賦值(第一個參數是開始分區的起始位置,第二個參數是結束分區的下一個位置)

修改序列

>>>name = list('Perl')>>>name[1:] = list('ython')>>>name['P', 'y', 't', 'h', 'o', 'n']

插入序列

>>>num = [1, 5]>>>num[1:1] = [2, 3, 4]>>>num[1, 2, 3, 4, 5]

刪除序列

>>>num = [1, 5]>>>num[1:1] = [2, 3, 4]>>>num[1, 2, 3, 4, 5]

4、append函數(改變原列表)(可以實現入棧操作)

在列表末尾添加新的對象

>>>num = [1, 5]>>>num[1:1] = [2, 3, 4]>>>num[1, 2, 3, 4, 5]


5、count函數

統計某個元素在列表中出現的次數

>>>['to', 'be', 'or', 'not', 'to', 'be' ].count('to')2>>>x = [[1, 2], 1, 1, [2, 1, [1, 2]]]>>>x.count(1)2>>>x.cout([1, 2])1

6、extend函數(修改原序列,串連操作產生新序列)

在列表末尾一次性追加另一個序列中的多個值(用新列表擴充原來的列表)

>>>a = [1, 2, 3]>>>b = [4, 5, 6]>>>a.extend(b)>>>a[1, 2, 3, 4, 5, 6]

用分區操作來實現上述步驟

>>>a = [1, 2, 3]>>>b = [4, 5, 6]>>>a[len(a):] = b>>>a[1, 2, 3, 4, 5, 6]

7、index函數

從列表中找出某個值第一個匹配項的索引位置

>>>num = ['one', 'two', 'three', 'four', 'five']>>>num.index['three']2>>>num[2]'three'


8、insert函數

將對象插入列表

>>> numbers = [1, 2, 3, 5, 6, 7]>>> numbers.insert(3, 'four')>>> numbers[1, 2, 3, 'four', 5, 6, 7]

9、pop函數(可以實現出棧操作)

移除列表中的一個元素(預設最後一個元素),並且返回該元素的值

>>> x = [1, 2, 3]>>> x.pop()3>>> x[1, 2]>>> x.pop(0)1>>> x[2]

10、remove函數(和pop區別,修改原序列,但不返回)

移除列表中某個值的第一個匹配項

>>> x = ['to', 'be', 'or', 'not', 'to', 'be']>>> x.remove('be')>>> x['to', 'or', 'not', 'to', 'be']

11、reverse函數

反向列表中元素

>>> x = [1, 2, 3]>>> x.reverse()>>> x[3, 2, 1]

12、sort函數(改變原列表,但是不返回)

對原列表進行排序

>>> x = [4, 6, 2, 3, 5, 1]>>> x.sort()>>> x[1, 2, 3, 4, 5, 6]

若不需要修改原序列(注意:如果這裡採用x = y的話,那麼x和y都指向同一個列表,即使操作時只對y排序,實際上x也會被排序)

方法一:棄置站台y,並對y進行排序

>>> x = [4, 6, 2, 3, 5, 1]>>> y = x[:]>>> y.sort()>>> x[4, 6, 2, 3, 5, 1]>>> y[1, 2, 3, 4, 5, 6]
方法二:使用sorted函數(返回一個已排序的副本,不改變原序列)

>>> x = [4, 6, 2, 3, 5, 1]>>> y = sorted(x)>>> y[1, 2, 3, 4, 5, 6]>>> x[4, 6, 2, 3, 5, 1]

關鍵字排序:key

長度(len)排序:

>>> x = ['bb', 'eeeee', 'a', 'dddd', 'ccc']>>> x.sort(key = len)>>> x['a', 'bb', 'ccc', 'dddd', 'eeeee']
關鍵字排序:reverse(簡單布爾排序,True從大到小, False從小到大)

>>> x = [4, 6, 2, 3, 5, 1]>>> x.sort(reverse = True)>>> x[6, 5, 4, 3, 2, 1]>>> y = [4, 6, 2, 3, 5, 1]>>> y.sort(reverse = False)>>> y[1, 2, 3, 4, 5, 6]


13、cmp(x, y)函數(可以自訂compare(x, y)函數,用來比較兩個元素)

比較兩個函數的大小

>>> cmp(43, 62)-1>>> cmp(64, 23)1>>> cmp(23, 23)0
結合sort函數,可以自訂排序,sort(cmp)

聯繫我們

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