武道之路-煉體期一重天(萬丈高樓平地起),一重萬丈高樓平地起

來源:互聯網
上載者:User

武道之路-煉體期一重天(萬丈高樓平地起),一重萬丈高樓平地起

列表一些簡單基礎的方法:

舉例一個列表

usernames=['李四','王二','張三','麻子']

索引:usernames[0]#從0開始,-1倒數(從左至右取)

分區:usernames[1:2]=['王二'],即是list[頭:尾:步長],顧頭不顧尾(左閉右開),usernames[2:],從第三個元素到最後一個

分區賦值:usernames[1:]=['sss'],即是替換下標從1開始的元素,

#len(usernames)->列表長度,max(usernames),min(usernames),列表中的極值

增:

#usernames.append('馬大哈')#在列表後面追加一個元素:['李四','王二','張三','麻子','馬大哈']

#usernames.insert(索引,要插入的內容),插在索引前面!!

刪:

#del usernames[2],刪除元素# del 是刪除變數

#usernames.pop(索引),不加索引預設刪除最後一個元素

# usernames.remove(索引),其實切片也可刪除

改:

#直接修改 usernames[1]='mmmm', 切片也可修改

查:

#usernames.index('元素')找出元素在列表中的位置(索引)

其它:

usernames.count(元素,start,end) 計算元素在列表出現的次數(在start和end間)

#usernames.extend(['2',33]),跟列表拼接很像

usenames.reserve() 反轉列表

usernames.sort() 排序

usernames.copy()或是import copy   copy.copy(usernames)  淺複製 #引用

import copy   copy.deepcopy(usernames) 深層複製,慎用(重新開闢一塊記憶體)

 

元組:元素不可變,('sss','ddd')

有2方法記住: index  count

 

聯繫我們

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