第3章 列表操作

3.1 列表定義
列表是由一系列按照特定順序排列的元素組成??梢园帜副恚瑪?shù)字0-9或所有家庭成員姓名的列表;也可以將任何東西加入到列表中
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)

3.2 訪問(wèn)列表元素

print(bicycles[0])
print(bicycles[0].title())
print(bicycles[-1])
3.3 修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles[0] = 'ducati'
print(motorcycles)
3.4 在列表末尾添加元素
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.append('ducati')
print(motorcycles)
3.5 在列表中插入元素
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
3.6 使用del語(yǔ)句刪除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
del motorcycles[0]
print(motorcycles)
3.7 使用方法pop() 刪除元素
方法pop() 可刪除列表末尾的元素
motorcycles = ['honda', 'yamaha', 'suzuki']
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
3.8 使用pop()彈出列表中任何位置處的元素
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
使用del 語(yǔ)句還是pop() 方法的判斷標(biāo)準(zhǔn):
如果你要從列表中刪除一個(gè)元素,且不再以任何方式使用它,就使用del 語(yǔ)句;如果你要在刪除元素后還能繼續(xù)使用它,就使用方法pop() 。
3.9 根據(jù)值刪除元素
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles.remove('ducati')
print(motorcycles)
3.10 使用方法sort() 對(duì)列表進(jìn)行永久性排序
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
3.11 使用函數(shù)sorted() 對(duì)列表進(jìn)行臨時(shí)排序
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(sorted(cars))
3.12 使用方法reverse()倒著打印列表
reverse() 不是指按與字母順序相反的順序排列列表元素,而只是反轉(zhuǎn)列表元素的排列順序
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
3.13 使用函數(shù)len() 可快速獲悉列表的長(zhǎng)度
3.14 遍歷整個(gè)列表
使用for 循環(huán)
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
3.15 創(chuàng)建數(shù)值列表
1)使用函數(shù)range()
for value in range(1,6):
print(value)
2)使用range()創(chuàng)建數(shù)字列表
numbers = list(range(1,6))
print(numbers)
3.16 列表解析
for 語(yǔ)句末尾沒(méi)有冒號(hào)
squares = [value**2 for value in range(1,11)]
print(squares)
3.17 切片
用于處理列表的部分元素
players = ['charles', 'martina', 'michael', 'florence', 'eli'] print(players[0:3])
print(players[1:4])
print(players[:4])
print(players[2:])
print(players[-3:])
3.18 復(fù)制列表
方法是同時(shí)省略起始索引和終止索引([:] )。
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. 訪問(wèn)列表元素 索引從0開(kāi)始而不是1 bicycles = ['trek','cannondale','red...
    LorryZ閱讀 207評(píng)論 0 0
  • 視頻看無(wú)聊了,也不想再看廖雪峰的教程了。 現(xiàn)在開(kāi)始記錄《Python編程:從入門(mén)到實(shí)踐》的筆記和進(jìn)度。 字符串: ...
    開(kāi)發(fā)猛男閱讀 133評(píng)論 0 0
  • 一、快捷鍵 ctr+b 執(zhí)行ctr+/ 單行注釋ctr+c ...
    o_8319閱讀 6,014評(píng)論 2 16
  • 1、去python官網(wǎng)下載安裝python即可在終端窗口運(yùn)行程序 2、下載文本編輯器 Sublime text3 ...
    夢(mèng)vctor閱讀 458評(píng)論 0 0
  • 年一度的生日又到了,我很興奮。 去年我十歲生日辦的很熱鬧,來(lái)來(lái)去去有將近二十人。結(jié)果我媽說(shuō)太煩了,況且現(xiàn)...
    佛語(yǔ)空靈閱讀 309評(píng)論 0 0

友情鏈接更多精彩內(nèi)容