python自學——列表

來源:互聯網
上載者:User

標籤:括弧   int   count   最好   好用   odi   特殊   選擇   rem   

#以下是我自己在聯絡列表中所編寫的語句:
names=["zangsan",‘lisi‘,‘wangermazi‘,‘Xiaoliuzi‘,‘dabiaoge‘,‘牛erbiaodi‘]
#----------0--------1---------2---------------3----------4---------5--------
print(names[2])#簡單取值
#取lisi和wangermazi
print(names[1:3])
#取後三位
print(names[-3:])
#去最後一位
print(names[5])#這個不太好,用了你就會失業,因為資料個數變了之後就不是最後一個了
print(names[-1])#去最後一位時最好用這個
#————————
# 增刪改查
#增
names.append(‘wangerxiao‘)#追加

print(names)
names.insert(1,‘panliujie‘)#插入insert( 位置-下標 ,‘資料’)
print(names)
#刪
del names[-1]
print(names)
names.remove(‘panliujie‘)#remove後面括弧內填寫的是資料-所刪除欄位
print(names)
names.pop(0)#pop後面寫下角標
print(names)
#改
names[0]=‘lisa‘#選擇元素下標賦值
print(names)
#查
print(names.index(‘dabiaoge‘))
#-----------
#除此之外還有有幾個:計數、清楚、反轉,排列、擴充(合并)
#計數
print(names.count("dabiaoge"))
names.reverse()
print(names)
names.sort()
print(names)#排序按照特殊字元>數字>大寫字母>小寫字母>中文
#names.clear()
#print(names)"""
#擴充extend
name2=[‘1‘,‘2‘,‘3‘]
names.extend(name2)
print(names,name2)
#-------------------------------------------------有問題歡迎指正------------------------------------------------------------------------------
#Author:皇城根兒

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.