Python列表的定義及使用
1、定義和使用列表
在Python中,列表是由一系列元素按照特定的順序構(gòu)成的數(shù)據(jù)結(jié)構(gòu),也就是說列表類型的變量可以存儲多個數(shù)據(jù),且可以重復(fù)。
1.1 定義列表
使用[]字面量語法定義變量,列表中的多個元素使用逗號,進(jìn)行分割,
示例代碼如下:
list1 = ["Hello", "一碗周", "你好"] list2 = [1, 2, 3, 4, 5] print(list1) # ['Hello', '一碗周', '你好'] print(list2) # [1, 2, 3, 4,5]
使用Python內(nèi)置的list將其他序列編程列表,示例代碼如下:
list1 = list(range(10))
list2 = list("hello")
print(list1) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list2) # ['h', 'e', 'l', 'l', 'o']
列表是一種可變的數(shù)據(jù)類型,也就是可以對列表的的元素可以進(jìn)行修改,這與字符串有顯著的差別,對字符串類型進(jìn)行修改后,都會返回新的字符串
1.2 訪問列表中的值
如果訪問列表中的某個值,使用下標(biāo)索引來訪問列表中的值,與字符串一樣使用方括號的形式截取字符,示例代碼如下:
list1 = ["Hello", "一碗周", "你好"] # 列表的索引 print(list1[1]) # 一碗周 # 列表的切片 print(list1[1:3]) # ['一碗周', '你好']
1.3列表的運(yùn)算符
表和字符串類型一樣,同樣支持拼接、重復(fù)、成員運(yùn)算等操作,
示例代碼如下:
list1 = ["Hello"]
list2 = ["World"]
list3 = [1, 2, 3, 4, 5]
list4 = list(range(1, 6))
list5 = list1 + list2 # ['Hello', 'World']
print(list5)
list6 = list1 * 3 # ['Hello', 'Hello', 'Hello']
list7 = list3 * 2 # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
print(list6)
print(list7)
print("W" in list1) # False
print("W" in list2) # False
# 列表的比較運(yùn)算
# 兩個列表比較相等性比的是對應(yīng)索引位置上的元素是否相等
print(list3 == list4) # True
list8 = list(range(1, 7))
print(list3 == list8) # False
1.4 列表元素的遍歷
遍歷列表同遍歷字符串是一樣的,示例代碼如下:
list1 = ["H", "e", "l", "l", "o"] # 方法1 for index in range(len(list1)): print(list1[index]) # 方法2 for ch in list1: print(ch)
2、列表的方法
2.1 添加和刪除元素
直接上代碼
list1 = ["cute", "beautiful", "一碗周"]
# append()在列表尾部添加元素
list1.append("lovely")
print(list1) # ['cute', 'beautiful', '一碗周', 'lovely']
# insert()在列表指定索引位置插入元素
list1.insert(2, "prefect")
print(list1) # ['cute', 'beautiful', 'prefect', '一碗周', 'lovely']
# remove()刪除指定元素
list1.remove("lovely")
print(list1) # ['cute', 'beautiful', 'prefect', '一碗周']
# pop()刪除指定索引位置的元素
list1.pop(2)
print(list1) # ['cute', 'beautiful', '一碗周']
# clear()清空列表中的元素
list1.clear()
print(list1) # []
在Python中也可以使用del關(guān)鍵字對列表元素進(jìn)行刪除,類似于pop,示例代碼↓
list1 = ["cute", "beautiful", "甜甜"] del list1[1] print(list1) # ['cute', '甜甜'] # 刪除整個列表 del list1 print(list1) # NameError: name 'list1' is not defined
2.2 元素位置和次數(shù)
使用index()來查找元素的位置,使用count()來統(tǒng)計元素出現(xiàn)的次數(shù)
list1 = ["beautiful", "cute", "beautiful",
'prefect', "beautiful", "一碗周", 'lovely']
# 查找"beautiful"第一次出現(xiàn)的位置
print(list1.index("beautiful")) # 0
# 從第四個元素以后查找"beautiful"最近一次出現(xiàn)的位置
print(list1.index("beautiful", 3)) # 4
# 統(tǒng)計"beautiful"出現(xiàn)的次數(shù)
print(list1.count("beautiful")) # 3
2.3 元素排序和反轉(zhuǎn)
使用sort()方法可以實(shí)現(xiàn)列表元素的排序,而reverse()方法可以實(shí)現(xiàn)元素的反轉(zhuǎn),示例代碼↓
list1 = ["cute", "beautiful", "一碗周"] list2 = list(range(10)) # 排序 list1.sort() print(list1) # ['beautiful', 'cute', '一碗周'] # 反轉(zhuǎn) list1.reverse() print(list1) # ['一碗周', 'cute', 'beautiful'] list2.reverse() print(list2) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] # 前面的操作原來的列表進(jìn)行修改,如果不讓原來的數(shù)據(jù)被破壞可以使用copy()備份一份 list3 = list2.copy() list3.sort() print(list2) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] print(list3) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2.4 小結(jié)


3、列表的生成式
要求:為字符串123和字符串ABC創(chuàng)建一個笛卡爾積構(gòu)成的列表,示例代碼如下:
原始方法:
a = "123" b = "ABC" list1 = [] for x in a: for y in b: list1.append(x + y) print(list1) # ['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']
生成列方法:
a = "123" b = "ABC" list1 = [x + y for x in a for y in b] print(list1) # ['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']
這中方法不僅代碼量少,而且性能上也要優(yōu)于普通的for循環(huán)和append追加的方式
4、嵌套的列表
因?yàn)榱斜砝锩娴淖兞靠梢源鎯Χ喾N數(shù)據(jù)類型,就出現(xiàn)了列表里面有列表的時候,稱之為列表的嵌套,示例代碼如下:
list1 = [["cute", "beautiful", "一碗周"], "cute", "beautiful", "一碗周"] print(list1[0]) # ['cute', 'beautiful', '一碗周'] print(list1[1]) # cute # 如果想要查看被嵌套的那個cute則需要使用多個索引值 print(list1[0][0]) # cute
不管嵌套多少都是同理的
到此這篇關(guān)于Python列表的定義及使用的文章就介紹到這了,更多相關(guān)Python列表定義及使用內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信