python編程從入門到實(shí)踐2——列表-創(chuàng)新互聯(lián)

目錄
  • 2. 列表介紹
    • 2.1 列表是什么
      • 2.1.1 訪問列表元素
      • 2.1.2 索引從0而不是1開始
    • 2.2 修改、添加和刪除元素
      • 2.2.1 修改列表元素
      • 2.2.2 在列表中添加元素
      • 2.2.3 從列表中刪除元素
    • 2.3 組織列表
      • 2.3.1 使用方法sort() 對列表進(jìn)行 永久性排序
      • 2.3.2 使用函數(shù)sorted() 對列表進(jìn)行 臨時排序
      • 2.3.3 倒著打印列表
      • 2.3.4 確定列表的長度
  • 3. 操作列表
    • 3.1 遍歷整個列表
    • 3.2 創(chuàng)建數(shù)值列表
      • 3.2.1 使用函數(shù)range()
      • 3.2.2 使用range() 創(chuàng)建數(shù)字列表
      • 3.2.3 對數(shù)字列表執(zhí)行簡單的統(tǒng)計(jì)計(jì)算
      • 3.2.4 列表解析
    • 3.3 使用列表的一部分
      • 3.3.1 切片
      • 3.3.2 復(fù)制列表

目前成都創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、北流網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。2. 列表介紹 2.1 列表是什么

用方括號([] )來表示列表,并用逗號來分隔其中的元素。列表 由一系列按特定順序排列的元素組成。你可以創(chuàng)建包含字母表中所有字母、數(shù)字。

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)

# ['trek', 'cannondale', 'redline', 'specialized']
2.1.1 訪問列表元素

只需將該元素的位置索引告訴Python即可。

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])

# trek

# 可使用方法title() 讓元素'trek' 的格式更整潔:
print(bicycles[0].title())

# Trek
2.1.2 索引從0而不是1開始

第一個列表元素的索引為0

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[1])
print(bicycles[3])

# cannondale
# specialized

將索引指定為-1,可讓Python返回最后一個列表元素

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[-1])

# specialized
2.2 修改、添加和刪除元素 2.2.1 修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki'] 
print(motorcycles)
# ['honda', 'yamaha', 'suzuki'] 

motorcycles[0] = 'ducati' 
print(motorcycles)
# ['ducati', 'yamaha', 'suzuki']
2.2.2 在列表中添加元素
  1. 在列表末尾添加元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
# ['honda', 'yamaha', 'suzuki']

motorcycles.append('ducati') 
print(motorcycles)

# ['honda', 'yamaha', 'suzuki', 'ducati']

motorcycles2 = []

motorcycles2.append('honda')
motorcycles2.append('yamaha')
motorcycles2.append('suzuki')

print(motorcycles2)
# ['honda', 'yamaha', 'suzuki', 'ducati']
  1. 在列表中插入元素
motorcycles = ['honda', 'yamaha', 'suzuki']

motorcycles.insert(0, 'ducati') ?
print(motorcycles)
# ['ducati', 'honda', 'yamaha', 'suzuki']
2.2.3 從列表中刪除元素
  1. 使用del 語句刪除元素,刪除后,你就無法再訪問它了。
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
# ['honda', 'yamaha', 'suzuki']

del motorcycles[0] 
print(motorcycles)
# ['yamaha', 'suzuki']

del 可刪除任何位置處的列表元素,條件是知道其索引。

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

del motorcycles[1]
print(motorcycles)

# ['honda', 'yamaha', 'suzuki']
# ['honda', 'suzuki']
  1. 使用方法pop() 刪除末尾元素

方法pop() 可刪除列表末尾的元素,并讓你能夠接著使用它。術(shù)語彈出 (pop)源自這樣的類比:列表就像一個棧,而刪除列表末尾的元素相當(dāng)于彈出棧頂元素。

motorcycles = ['honda', 'yamaha', 'suzuki'] 
print(motorcycles)

popped_motorcycle = motorcycles.pop() 
print(motorcycles) 
print(popped_motorcycle) 

# ['honda', 'yamaha', 'suzuki']
# ['honda', 'yamaha']
# suzuki
motorcycles = ['honda', 'yamaha', 'suzuki']

last_owned = motorcycles.pop()
print("The last motorcycle I owned was a " + last_owned.title() + ".")

# The last motorcycle I owned was a Suzuki.
  1. 彈出列表中任何位置處的元素
    pop()+索引
motorcycles = ['honda', 'yamaha', 'suzuki']

first_owned = motorcycles.pop(0) ?
print('The first motorcycle I owned was a ' + first_owned.title() + '.')

# The first motorcycle I owned was a Honda.
  1. 根據(jù)值刪除元素

方法remove()

motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)

motorcycles.remove('ducati') 
print(motorcycles)

# ['honda', 'yamaha', 'suzuki', 'ducati']
# ['honda', 'yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] ?
print(motorcycles)

too_expensive = 'ducati' ?
motorcycles.remove(too_expensive) ?
print(motorcycles)
print("\nA " + too_expensive.title() + " is too expensive for me.") # \n 回車

# ['honda', 'yamaha', 'suzuki', 'ducati']
# ['honda', 'yamaha', 'suzuki']

# A Ducati is too expensive for me.
2.3 組織列表 2.3.1 使用方法sort() 對列表進(jìn)行 永久性排序

sort(),永久性地修改了列表元素的排列順序。

cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort() ?
print(cars)

# ['audi', 'bmw', 'subaru', 'toyota']

相反的順序,向sort() 方法傳遞參數(shù)reverse=True

cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)

# ['toyota', 'subaru', 'bmw', 'audi']
2.3.2 使用函數(shù)sorted() 對列表進(jìn)行 臨時排序

調(diào)用函數(shù)sorted() 后,列表元素的排列順序并沒有變。如果你要按與字母順序相反的順序顯示列表,也可向函數(shù)sorted() 傳遞參數(shù)reverse=True

cars = ['bmw', 'audi', 'toyota', 'subaru']

print("Here is the original list:") ?
print(cars)

print("\nHere is the sorted list:") ?
print(sorted(cars))

print("\nHere is the original list again:") ?
print(cars)

output:

Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']  # 臨時排序

Here is the original list again: ?
['bmw', 'audi', 'toyota', 'subaru']  # 原始順序
2.3.3 倒著打印列表

reverse() ,永久性地修改列表元素的排列順序,但可隨時恢復(fù)到原來的排列順序,為此只需對列表再次調(diào)用reverse() 即可。

cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)

cars.reverse()
print(cars)

# ['bmw', 'audi', 'toyota', 'subaru']
# ['subaru', 'toyota', 'audi', 'bmw']
2.3.4 確定列表的長度

len()

>>>cars = ['bmw', 'audi', 'toyota', 'subaru']
>>>len(cars)
4
3. 操作列表 3.1 遍歷整個列表

for循環(huán)

magicians = ['alice', 'david', 'carolina'] ?
for magician in magicians: ?
    print(magician) ?

alice
david
carolina
3.2 創(chuàng)建數(shù)值列表 3.2.1 使用函數(shù)range()
for value in range(1,5):
    print(value)
# 輸出
1
2
3
4

for value in range(1,6):
    print(value)
1
2
3
4
5
3.2.2 使用range() 創(chuàng)建數(shù)字列表
numbers = list(range(1,6))
print(numbers)

# [1, 2, 3, 4, 5]

even_numbers = list(range(2,11,2))
print(even_numbers)

# [2, 4, 6, 8, 10]
3.2.3 對數(shù)字列表執(zhí)行簡單的統(tǒng)計(jì)計(jì)算
>>>digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>>min(digits)
0

>>>max(digits)
9

>>>sum(digits)
45
3.2.4 列表解析

列表解析 將for 循環(huán)和創(chuàng)建新元素的代碼合并成一行,并自動附加新元素。

squares = [value**2 for value in range(1,11)]
print(squares)

# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
3.3 使用列表的一部分 3.3.1 切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3]) ?
['charles', 'martina', 'michael']

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])
['martina', 'michael', 'florence']

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])
['charles', 'martina', 'michael', 'florence']
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])
['michael', 'florence', 'eli']
print(players[-3: -1])
['michael', 'florence']
3.3.2 復(fù)制列表

要復(fù)制列表,可創(chuàng)建一個包含整個列表的切片,方法是同時省略起始索引和終止索引([:])。這讓Python創(chuàng)建一個始于第一個元素,終止于最后一個元素的切片,即復(fù)制整個列表。

my_foods = ['pizza', 'falafel', 'carrot cake'] ?
friend_foods = my_foods[:] ?

print("My favorite foods are:")
print(my_foods)

print("\nMy friend's favorite foods are:")
print(friend_foods)

輸出:

My favorite foods are:
['pizza', 'falafel', 'carrot cake']

My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake']

另外

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:] ?

my_foods.append('cannoli') ?
friend_foods.append('ice cream') ?

print("My favorite foods are:")
print(my_foods)

print("\nMy friend's favorite foods are:")
print(friend_foods)

# My favorite foods are:
# ['pizza', 'falafel', 'carrot cake', 'cannoli'] ?

# My friend's favorite foods are:
# ['pizza', 'falafel', 'carrot cake', 'ice cream'] ?

錯誤方法:

my_foods = ['pizza', 'falafel', 'carrot cake']

#這行不通
friend_foods = my_foods ?

my_foods.append('cannoli')
friend_foods.append('ice cream')

print("My favorite foods are:")
print(my_foods)

print("\nMy friend's favorite foods are:")
print(friend_foods)

輸出:

My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

本文名稱:python編程從入門到實(shí)踐2——列表-創(chuàng)新互聯(lián)
分享URL:http://muchs.cn/article28/dddscp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、虛擬主機(jī)手機(jī)網(wǎng)站建設(shè)、電子商務(wù)、品牌網(wǎng)站建設(shè)云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)