python資料結構(二)------列表

來源:互聯網
上載者:User

標籤:多個   append   one   names   ant   error   資料結構   value   sam   

  本文將重點梳理列表及列表操作。

2.1  list函數

2.2  基本欄表操作

2.3  列表方法

 

2.1  list函數

  >>>list(‘hello‘)

  [‘h‘,‘e‘,‘l‘,l‘,‘o‘]

註:list函數適用於所有類型的序列,而不只是字串。

2.2  基本欄表操作

  2.2.1  改變列表:元素賦值

  >>>x = [1,3,5]

  >>>x[0] = ‘a‘

  >>>x

  [‘a‘,3,5]

註:不能對不存在的元素賦值,即x[0]~x[2]存在,可賦值,x[3]不存在,不可賦值。

  2.2.2  刪除元素

  >>>names = [‘jack‘,‘bob‘,‘tom‘,‘alice‘]

  >>>del names[1]

  >>>names

  [‘jack‘,‘tom‘,‘alice‘]

  2.2.3  分區賦值

  >>>name = ‘anthony‘

  >>>namelist = list(name)

  [‘a‘,‘n‘,‘t‘,‘h‘,‘o‘,‘n‘,‘y‘]

  #步長為1時,替換原分區位置為賦值列表

  >>>namelist[1:3] = [‘a‘,‘b‘,‘c‘]

  [‘a‘, ‘a‘, ‘b‘, ‘c‘, ‘h‘, ‘o‘, ‘n‘, ‘y‘]

  #步長為2時,賦值列表長度必須與原分區元素數一致

  >>>namelist[::2] = [1,2,3]

  Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
  ValueError: attempt to assign sequence of size 3 to extended slice of size 4

  >>>namelist[::2] = [1,2,3,5]

  >>>namelist

  [1, ‘a‘, 2, ‘c‘, 3, ‘o‘, 5, ‘y‘]

  #分區操作可以實現刪除序列功能

  >>>namelist[1:] = []

  >>>namelist

  [‘a‘]

2.3  列表方法

  2.3.1  append

  #append方法實現列表末尾追加新的對象;

   >>>lst = [‘a‘,‘b‘,4]

   >>>lst.append(‘a‘)

   [‘a‘, ‘b‘, 4, ‘a‘]

  #直接修改原列表

  2.3.2  count

  #count方法統計某個元素在列表中出現的次數;

   >>>lst.count(‘a‘)

   2

   

  2.3.3  extend

    #extend方法實現在列表的末尾追加另一個序列中的多個值;

    >>>lst2 = [1,2,3]

    >>>lst.extend(lst2)

    >>>lst

    [‘a‘, ‘b‘, 4, ‘a‘, 1, 2, 3]

    #extend方法不同於級聯操作”+“,extend方法是在原列表直接修改,而“+”操作是返回一個新的列表

    >>>a = [‘a‘,‘a‘,‘b‘]

    >>>b = [1,2,3]

    >>>a+b

    [‘a‘,‘a‘,‘b‘,1,2,3]

    >>>a

    [‘a‘,‘a‘,‘b‘]

  2.3.4  index

    #index方法用於尋找列表中某個值第一個匹配項的索引位置;

    >>>names = [‘jack‘,‘tom‘,‘alice‘,‘rose‘,‘timor‘]

    >>>names.index(‘jack‘)

    0

    >>>names.index(‘sam‘)

    Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
    ValueError: ‘sam‘ is not in list

  2.3.5  insert

    #insert方法用於將對象插入列表中;

    >>>numbers = [1,2,3,4,5,6,7]

    >>>numbers.insert(3,‘a‘)

    >>>numbers

    [1,2,3,‘a‘,4,5,6,7]

    #同樣可以用分區操作完成

    >>>numbers[3:3] = [‘a‘]

    [1,2,3,‘a‘,‘a‘,4,5,6,7]

  2.3.6  pop

    #pop方法會移除列表中的一個元素(預設最後一個);

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

    >>>x.pop()

    3

    >>>x

    [1,2]

    >>>x.pop(0)

    1

    >>>x

    [2]

  2.3.7  remove

   #remove方法用於移除列表中某個值的第一個匹配項;

    >>>x = [‘alice‘,‘tome‘,‘time‘,‘jack‘]

    >>>x.remove(‘alice‘)

    [tome‘,‘time‘,‘jack‘]

  2.3.8  reverse

    #reverse方法將列表中的元素逆向存放;

    >>>x = [1,3,5]

    >>>x.reverse()

    >>>x

    [5,3,1]

  2.3.9  sort

    #sort方法在原位置進行排序;

    >>>x = [4,6,9,7,8]

    >>>x.sort()

    >>>x

    [4,6,7,8,9]

    #sort方法無傳回值,因此,y = x.sort() 傳回值為None

 

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.