python列表筆記

2021-10-22 13:28:56 字數 1536 閱讀 7938

# 列表:列表python中最基本的資料結構

# 序列中每個值都有對應的位置值

# python 有六個序列的內建型別,但是最常見的操作 索引 切片 加 乘 檢查

# 列表是最常見的常用的python資料型別它可以作為乙個方括號內的逗號分隔值出現

# 訪問

list = ['red', 'green', 'blue', 'yellow', 'white', 'black']

print(list[0])

print(list[1])

print(list[2])

print("-------------------")

print(list[-1])

print(list[-2])

print(list[-3])

# 使用下標索引來訪問列表中的值,同樣你也可以使用方括號的形式擷取字元

temp = list[0:4]

print(temp) # ['red', 'green', 'blue', 'yellow']

# 列表中使用的是[a:b] 形式

temp = list[1:-2]

print(temp)

list2 = [1, 2, 3, 4, "a", "b", 3, 3, "a"]

print(list2)

print(list2)

# count(obj) 統計某個元素在列表**現的次數

print(list2.count(3))

# index() 函式用於從列表中找出某個值第乙個匹配項的索引位置

# 語法: index()方法語句

print("3的索引位置", str(list2.index("a")))

# insert(index,obj) 用於將指定物件插入列表的指定位置 沒有返回值

# pop()函式 用於移除列表中的乙個元素,(預設最後乙個元素)。並且返回該元素的值

# pop()方法語法

list2 = ["google", "run", "sun", "play", "dance", "sing", "sang"]

print(list2.pop(3))

print(list2)

# remove() 函式用於移除列表中某個值的第乙個匹配項

list2.remove("google")

print(list2)

# reverse() 反向列表中的元素

list2.reverse()

print(list2)

# sort 排序

arr = [1, 4, 2, 6, 2, 4, 2]

arr.sort(reverse=true)

print(arr)

ar = [1, 4, 2, 6, 2, 4, 2]

print(sorted(ar))

python列表筆記

1.列表可以包含任意字元 結構 型別的資料 例 shuaige 帥 有錢 高 95599,true,等等 print shuaige print shuaige 輸出顯示如下 帥 有錢 高 95599,true,等等 帥 有錢 高 95599,true,等等 牛鼻 檢測 解藥 是否在inventor...

python 列表(筆記)

a 1,2,3 b a b只是貼在a 上的標籤,b變a也變 c a 用切片實現 這才是真正的複製,給 c 也分了記憶體 a.extent element1,element2,a.insert indx,element a.remove element a.pop index del a 0 del ...

Python列表筆記

使用for迴圈生成列表元素,可以迴圈巢狀。舉例 urls a.get href for a in title.find all a 0 for title in title python中符合序列的有序序列都支援切片 slice 例如列表,字串,元組。格式 start end step start ...