Python基礎 列表 元組 集合 字典

2021-09-25 13:12:40 字數 4586 閱讀 2501

1、列表(list)

列表中的資料型別可以包含多種,包含數字、字串、字典、布林型等,並且列表中還可以巢狀列表

print(type([1, 2, 3, 4, 5, 6]))

print(type(['hello', 'world', 1, 6]))

print(type(['hello', 1, 2, true, 'a']))

print(type([['hello', 1, 2, 3], [true, 1]]))

# 列表中巢狀列表

print(type([['hello', 'world'], [1, 2, 3], [true, false]]))

print(type([['hello', 'world'], [1, 2, 3], [true, false], ]))

結果為:

print(["新月打擊", "蒼白之瀑", "月之降臨", "月神衝刺"][0])

print(["新月打擊", "蒼白之瀑", "月之降臨", "月神衝刺"][3])

print(["新月打擊", "蒼白之瀑", "月之降臨", "月神衝刺"][0:2])

print(["新月打擊", "蒼白之瀑", "月之降臨", "月神衝刺"][-1:])

print(["新月打擊", "蒼白之瀑", "月之降臨", "月神衝刺"][-1])

結果為:

新月打擊

月神衝刺

[『新月打擊』, 『蒼白之瀑』]

[『月神衝刺』]

月神衝刺

可以發現,單一數字的索引來訪問的時候,得到的是乙個元素;用冒號的方式來訪問,得到的是乙個列表,哪怕這個列表只有乙個元素

print(["新月打擊", "蒼白之瀑", "月之降臨", "月神衝刺"] + ["點燃", "打擊"])

print(["點燃", "打擊"]*3)

結果為:

[『新月打擊』, 『蒼白之瀑』, 『月之降臨』, 『月神衝刺』, 『點燃』, 『打擊』]

[『點燃』, 『打擊』, 『點燃』, 『打擊』, 『點燃』, 『打擊』]

注意: print([「點燃」, 「打擊」]-[「點燃」]) # 輸出錯誤

2、元組(tuple)

元組中的資料型別也可以包含多種

print((1, 2, 3, 4, 5))

print((1, '-1', true, 'morning', , [1, 'hello', '1']))

結果為:

(1, 2, 3, 4, 5)

(1, 『-1』, true, 『morning』, , [1, 『hello』, 『1』])

print((1, 2, 3, 4, 5)[0])

print((1, 2, 'morning', 4, true)[0:3])

print((1, 2, 'morning', 4, true)[-1:])

print((1, 2, 'morning', 4, true)[-1])

print((1, 2, 3) + (4, 5, 6))

print((1, 2, 3) * 3)

print((1, 2)[-1:])

結果為:

1

(1, 2, 'morning')

(true,)

true

(1, 2, 3, 4, 5, 6)

(1, 2, 3, 1, 2, 3, 1, 2, 3)

(2,)

注意:單一數字的索引來訪問的時候,得到的是乙個元素;用冒號的方式來訪問,得到的是乙個元組,哪怕這個元組只有乙個元素

print(type((1, 2, 3)))

print(type(1))

print(type((1))) # 小括號代表運算符號時,比如(1+1)*2,和元組的括號有衝突,python當括號裡面只有乙個元素時,規定為運算符號

# 定義只有乙個元素的元組

print(type((1,)))

# 表示乙個空的元組

print(type(()))

結果為:

print(type("hello"))

print(type(("hello")))

print(type([1, 2, 3]))

print(type([1]))

結果為:

3、str list tuple ---->序列

print((1, 2, 3)[2])

print('hello world'[2])

結果為:

3

l

print([1, 2, 3, 4, 5][0:3])

print([1, 2, 3, 4, 5][-1:])

print("hello world"[0:8:2])

結果為:

[1, 2, 3]

[5]hlow

print(3 in [1, 2, 3, 4, 5])

print(3 not in [1, 2, 3, 4, 5])

結果為:

true

false

print(len([1, 2, 3, 4, 5, 6]))

print(len("hello world"))

print(max([1, 2, 3, 4, 5, 6]))

print(min([1, 2, 3, 4, 5, 6]))

print(max("hello world"))

print(min("helloworld"))

結果為:

6116

1wh

求取ascii 的求取

print(ord('h'))

print(ord('w'))

print(ord(' '))

結果為:

72

11932

4、 集合

print(type())

# print([0]) # 錯誤

結果為:

print()
結果為:

print(len())
結果為:

3
print(-)

print( & )
結果為:

print( | )
結果為:

print(type({}))

print(type(set()))

結果為:

}
約個拓展練習可好:

快樂的leetcode — 14. 最長公共字首 方法2中包含了集合的巧妙使用

5、字典(dict)

形式: 。 可以有很多個key和value,屬於集合型別(set),不是序列型別

print(['q'])

# 相同的key情況,儘管有結果輸出

print(['q'])

# 驗證發現,已經存在丟失

print()

結果為:

新月打擊

蒼白之瀑

print()

# key必須是不可變的型別, 數字1,字串"1"

# 反例 print()

結果為:

print(, 'e': "月神衝刺"})
結果為:

, 'e': '月神衝刺'}
print(type({}))
結果為:

大家加油!

第4章 python中表示「組」的概念與定義.mp4

python基礎 列表,集合,元組,字典

目錄 1,如何安裝python3.6 2,列表 3,元組 4,集合 5,字典 解壓安裝包到 opt目錄 安裝編譯過程中需要的依賴包有 gcc,zlib,zlib devel,openssl devel 進入安裝包進行編譯 cd opt python3 prefix安裝路徑 with ssl 新增ss...

python列表 元組 集合

型別 list tuple set特點 有序,可重複,內容可變,可通過下標獲取元素 有序,可重複,內容不可變,可通過下標獲取元素 無序 不可重複 定義 張三 李四 王五 張三 李四 王五 只包含乙個元素的元組 張三 空 set 查詢list index tuple index 修改list inde...

python基礎之列表元組字典集合

列表,元組,字典,集合 列表可以刪除,新增,替換,重排序列表中的元素,而元組一旦確定,不能在更新元組中的資料。建立字典容器中儲存著一系列的key value對,通過key來索引value 集合是不重複元素的無序組合,集合會自動忽略重複的資料 建立列表 方括號法或指明型別法list 建立元組 圓括號法...