python列表操作

來源:互聯網
上載者:User

標籤:表示   blog   ever   body   ==   move   添加   app   class   

count():

    統計列表中元素重複的次數

name = ["1", "2", "2", "1"]print(name.count("1"))

len():

    就算“長度”

    如果要統計列表中所有的元素的個數那麼用count()函數就不行了,因為count()至少要輸入一個參數,此時就可以用到len()函數了,

name = ["1", "2", "2", "1"]print(len(name))

列表的切片:

    直接用列表名+[ ](裡面的第一個參數表示起始位置,第二個參數是步長,預設是1,可以省略,第三個參數是停止位置,應該是停止位置的前一個,是不包括前一個位置的)

append():

    在列表中的最後位置添加元素

name = ["4", "3", "2", "1"]name.append("5")print(name)
name = ["4", "3", "2", "1"]name.reverse()print(name)

 

insert():

    可以在任意位置插入新元素

name = ["4", "3", "2", "1"]name.insert(1,"zhanghuarong")print(name)

替換:

name = ["4", "3", "2", "1"]name[2] = "liming"print(name)

remove():

name = ["4", "3", "2", "1"]name.remove("3")print(name)

reverse():

列表元素翻轉

name = ["4", "3", "2", "1"]name.reverse()print(name)

sort():

升序排序

name = ["4", "3", "1", "2"]name.sort()print(name)

extend():

name = ["4", "3", "1", "2"]name2 = [1, 2, 3, 4]name.extend(name2)print(name)

clear():

清除列表所有的元素

name = ["4", "3", "1", "2"]name.clear()print(name)

清除列表元素以後就是一個空列表了。

 

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.