Python中list的含義是什嗎?一文搞懂Python中list的方法和用法

來源:互聯網
上載者:User

Python中的listpython中的常用資料類型,其為python中內建類,繼承自object。接下來全面介紹list的常見方法及自己實作類別Python list功能的類

定義list

>>> li = ["a", "b", "mpilgrim", "z", "example"]>>> li ['a', 'b', 'mpilgrim', 'z', 'example']>>> li[0]'a' >>> li[4]'example'

負的list 索引

>>> li ['a', 'b', 'mpilgrim', 'z', 'example']>>> li[-1] 'example' >>> li[-3] 'mpilgrim' >>> li ['a', 'b', 'mpilgrim', 'z', 'example']>>> li[1:3]  ['b', 'mpilgrim'] >>> li[1:-1] ['b', 'mpilgrim', 'z'] >>> li[0:3]  ['a', 'b', 'mpilgrim']

向 list 中增加元素

>>> li ['a', 'b', 'mpilgrim', 'z', 'example']>>> li.append("new")>>> li ['a', 'b', 'mpilgrim', 'z', 'example', 'new']>>> li.insert(2, "new")>>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']>>> li.extend(["two", "elements"]) >>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']

遍曆

     list1 = [x for x in range(0,10,2)]                # 方法一            for i in range(len(list1)):                    print(list1[i], end=' ')                  # 方法二            for x in list1:                print(x, end=' ')                  # 方法三            for ind,value in enumerate(list1):                     print(ind, value, sep='=', end = ' ')

以上為個人理解,如有誤解或錯誤,請指正

聯繫我們

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