python 第3章列表介紹

2021-09-29 18:30:25 字數 4024 閱讀 7250

#3.1什麼是列表

#用來表示列表,並用逗號來分隔其中元素

bicycles = [『trek』,『cannondale』,『redline』,『specialized』]

print(bicycles)

#如果讓python將列表列印出來,python將列印列表的內部表示,包括括號

#3.1.1訪問列表元(告訴python位置)

#從自行車種類中提出第一種自行車

bicycles = [『trek』,『cannondale』,『redline』,『specialized』]

print(bicycles[0])

#python獲取列表時只返回該元素但不包括方括號和引號

#加title元素使得結果更簡潔

bicycles = [『trek』,『cannondale』,『redline』,『specialized』]

print(bicycles[0].title())

#3.1.2索引從0開始而不是1

bicycles = [『trek』,『cannondale』,『redline』,『specialized』]

print(bicycles[1])

bicycles = [『trek』,『cannondale』,『redline』,『specialized』]

print(bicycles[3])

#通過將索引定位-1來返回到最後乙個

bicycles = [『trek』,『cannondale』,『redline』,『specialized』]

print(bicycles[-1])

#這種語法非常有用,因為經常需要在不知道列表長度的情況下訪問最後乙個元素)

#這種約定也適用於其他負數,eg.-2為返回倒數第二個列表元素,-3就是倒數第三個…

#3.1.3使用列表中的各個值

bicycles = [『trek』,『cannondale』,『redline』,『specialized』]

message = "my first bicycle was a " + bicycles[0].title() + 「.」

print(message)

#3.2修改、新增和刪除元素

#3.2.1修改列表元素

motorcycles = [『honda』,『yamaha』,『suzuki』]

print(motorcycles)

motorcycles[0] = 『ducati』

print(motorcycles)

#2.在列表末中插入元素insert()

#inset()可以在列表中的任何位置新增元素。為此,你需要制定新元素的索引和值

motorcycles = [『honda』,『yamaha』,『suzuki』]

motorcycles.insert(0,『ducati』)

print(motorcycles)

#3.2.3從列表中刪除元素

#1.用del語句刪除元素(知道位置)

motorcycles = [『honda』,『yamaha』,『suzuki』]

print(motorcycles)

del motorcycles[0]

print(motorcycles)

#用del語句可刪除任何位置的列表元素,條件是知道其索引

motorcycles = [『honda』,『yamaha』,『suzuki』]

print(motorcycles)

del motorcycles[1]

print(motorcycles)

#2.用pop()語句刪除末尾元素

#從motorcycles列表中彈出一款電單車

motorcycles = [『honda』,『yamaha』,『suzuki』]

print(motorcycles)

popped_motorcycles = motorcycles.pop()

print(motorcycles)

print(popped_motorcycles)

#假設電單車是按照購買時間儲存的,就可以用pop()列印一條資訊,指出最後購買的是哪款

motorcycles = [『honda』,『yamaha』,『suzuki』]

last_owned = motorcycles.pop()

print("the last motorcycle i owned was a " + last_owned.title() + 「.」)

#3.彈出列表中任何位置處的元素

motorcycles = [『honda』,『yamaha』,『suzuki』]

first_owned = motorcycles.pop(0)

print("the first motorcycle i owned was a " + first_owned.title() + 「.」)

#注意!使用pop()時被彈出了的元素就不在了

#如果你要從列表中刪除乙個元素,且不再以任何方式使用它就用del語句;如果你要在刪除元素之後還能繼續使用就用pop()

#根據值刪除元素

#不知道要從列別中刪除值的位置,如果只知道刪除的元素的值,可使用remove()

motorcycles = [『honda』,『yamaha』,『suzuki』,『ducati』]

print(motorcycles)

motorcycles.remove(『ducati』)

print(motorcycles)

#指出將其從列表中刪除的原因

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.")

#3.3組織列表

#3.3.1使用方法sort()對列表進行永久性排序(按照字母順序)

cars = [『bmw』,『audi』,『toyota』,『subaru』]

cars.sort()

print(cars)

#反向排序,只需要向sort()方法傳遞引數reverse=true

cars = [『bmw』,『audi』,『toyota』,『subaru』]

cars.sort(reverse=true)

print(cars)

#3.3.2使用方法sorted()對列表進行臨時排序

#sorted()能夠按特定的順序顯示列表元素,同時不影響他們在列表中的原始排列順序

cars = [『bmw』,『audi』,『toyota』,『subaru』]

print(「here is the original list:」)

print(cars)

print("\nhere is the sorted list:")

print(sorted(cars))

print(「here is the original list again:」)

print(cars)

#呼叫函式sorted()後,列表元素排列順序沒有變,如果要按與字母順序相反順序顯示列表,也可以用sorted()傳遞引數reverse=true

#3.3.3倒著列印列表reverse()

cars = [『bmw』,『audi』,『toyota』,『subaru』]

print(cars)

cars.reverse()

print(cars)

#此時不在是字母順序是原本的排列順序

#reverse()是永久性的,但可以隨時恢復排列順序,只需對列表再次呼叫reverse()即可

#3.3.4確定列別長度len()

cars = [『bmw』,『audi』,『toyota』,『subaru』]

len(cars)

4

Python學習筆記第3章 操作列表 1

一 遍歷整個列表 我們經常需要遍歷列表的所有元素,為了避免大量的重複 這裡我麼使用for迴圈來解決這些問題。實現 name chenchen weipeng jiangnan suqin for person in name print person 執行結果如下 chenchen weipeng ...

第3章 MFC原理介紹

1.使用ctime類顯示當前時間 void c lg ontest 2.wm command wparam記錄單擊的按鈕或者選單項等id號碼 wm lbuttondown lparam記錄單擊介面的 x,y 座標等 3.cwnd setwindowtext sets the window s tit...

python第4章操作列表練習

4 3 數到 20 使用乙個 for 迴圈列印數字 1 20 含 for value in range 1,21 忘加冒號 print value 4 4 一百萬 建立乙個列表,其中包含數字 1 1 000 000,再使用乙個 for 迴圈將這 些數字列印出來 如果輸出的時間太長,按 ctrl c停...