容器結構

tags: Python

list 串列

串列名 = [10, '字串', 3.14, '都行']

範例: 班級名條

使用串列前:

nameList = '大明\n小明\n' nameList += '小白\n' + '大壯\n' print(nameList) # output: # 大明 # 小明 # 小白 # 大壯

使用串列後:

nameList = ['大明', '小明', '小白'] nameList.append('大壯') print(nameList) # 輸出結果: ['大明', '小明', '小白', '大壯'] anotherList = ['小美', '阿財'] nameList += anotherList print(nameList) # 輸出結果: '大明', '小明', '小白', '大壯', '小美', '阿財']

如果想添加單一元素可以用 .append()
如果想合併串列則可以直接 +=

常見方法

testListOne = list([10,'字串', 3.14,'都行']) print(testListOne) # 輸出結果: [10, '字串', 3.14, '都行'] testListTwo = list('Hello') #將字串拆解成字元存入 print(testListTwo) # 輸出結果: ['H', 'e', 'l', 'l', 'o'] testListThree = list(range(10)) #依序加入10個數字(0開始) print(testListThree) # 輸出結果: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] testListFour = ['阿昌'] * 3 print(testListFour) # 輸出結果:['阿昌', '阿昌', '阿昌']

索引用法

nameList = ['大明', '小明', '小白', '大壯', '小美', '阿財', '阿昌'] # 索引用法 print(nameList[0]) # 輸出結果: 大明 print(nameList[5]) # 輸出結果: 阿財 print(nameList[-1]) #倒數第一 # 輸出結果: 阿昌 print(nameList[-2]) #倒數第二 # 輸出結果: 阿財 newList = nameList[1:3] #索引1~2(小於3) print(newList) # 輸出結果: ['小明', '小白'] newList = nameList[:3] #索引0~2(小於3) print(newList) # 輸出結果: ['大明', '小明', '小白'] newList = nameList[1:] #索引1~最後 print(newList) # 輸出結果: ['小明', '小白', '大壯', '小美', '阿財', '阿昌'] newList = nameList[::] #索引0~最後 print(newList) # 輸出結果: ['大明', '小明', '小白', '大壯', '小美', '阿財', '阿昌'] # Python 獨有 numList = list(range(10)) print(numList[::3]) #0開始遞增3的索引值 # 輸出結果: [0, 3, 6, 9] print(numList[::-3]) #最後開始遞減3的索引值 # 輸出結果:[9, 6, 3, 0] # 更改特定索引元素 nameList[1] = '阿昌' print(nameList) # 輸出結果: ['大明', '阿昌', '小白', '大壯', '小美', '阿財', '阿昌'] # 計算特定元素出現次數 nameList.count('阿昌') # 輸出結果: 2 # 查詢元素索引值最小的數 nameList.index('小白') # 輸出結果: 2 # 在指定索引加入特定元素 nameList.insert(1,'添財') print(nameList) # 輸出結果: ['大明', '添財', '阿昌', '小白', '大壯', '小美', '阿財', '阿昌'] # 刪除特定元素(索引值最小) nameList.remove('') print(nameList) # 輸出結果: ['國瑜', '啟丞', '永康', '巧芯', '寬恆', '崐萁'] # 串列反轉 nameList.reverse() print(nameList) # 輸出結果: ['崐萁', '寬恆', '巧芯', '永康', '啟丞', '國瑜'] # 查看某個值是否在該串列中 nameList = ['國瑜', '啟丞', '永康', '巧芯', '寬恆', '崐萁'] '伯洋' in nameList # 輸出結果: False '國瑜' in nameList # 輸出結果: True # 清空串列 nameList.clear() print(nameList) # 輸出結果: []

Tuple 元組

除了串列之外,也有其他的容器。

testTuple = ('int', 'str', 'float', '都行') testTuple = 'int', 'str', 'float', '不加括號預設為元組' testTuple = '如果只有一個資料,一定要再加一個逗號',

需注意的Tuple內的參數是不可以更改的

常見用法

# 其他建立Tuples的方法 testTupleOne = tuple(['int','str','float','都行']) print(testTupleOne) # 輸出結果: ('int','str','float','都行') testTupleTwo = tuple('Hello') print(testTupleTwo) # 輸出結果: ('H', 'e', 'l', 'l', 'o') testTupleThree = tuple(range(10)) print(testTupleThree) # 輸出結果: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) testTupleFour = ('int', 'str') * 3 print(testTupleFour) # 輸出結果: ('int', 'str', 'int', 'str', 'int', 'str') # 索引用法 NameList = ('國瑜', '啟丞', '永康', '巧芯', '寬恆', '崐萁') print(NameList[0]) # 輸出結果: 國瑜 print(NameList[5]) # 輸出結果: 崐萁 print(NameList[2:4]) #索引2~3(小於4) # 輸出結果: ('永康', '巧芯') print(NameList[:3]) #索引0~2(小於3) # 輸出結果: ['國瑜', '啟丞', '永康'] print(NameList[1:]) #索引1~最後 # 輸出結果: ['啟丞', '永康', '巧芯', '寬恆', '崐萁'] print(nameList[:]) #索引0~最後 # 輸出結果: ['國瑜', '啟丞', '永康', '巧芯', '寬恆', '崐萁'] # 計算特定元素出現次數 print(nameList.count('永康')) # 輸出結果: 1 # 查詢元素索引值最小的數 print(nameList.index('永康')) # 輸出結果: 1

Dictionary

字典名 = {'key1': 2024, 'key2': 'value2', 'key3': 3.14159}

可以想像key為字典的索引,而value則是索引值;所以索引不可重複,但索引值可重複。

範例: 馬斯克個人資訊

dic = {'人物姓名':'Elon Reeve Musk', '出生年份':1971, '出生國家':'南非', '擁有國籍':['南非', '加拿大', '美國'], '目前職業':['Tesla CEO', 'SpaceX CEO'] } print(dic['英文姓名'])

Str 字串補充

字串裁切

s = 'Hello World!' c = s[0] print(c) # 輸出結果: H c = s[-1] print(c) # 輸出結果: ! c = s[2:4] # 複製第3到第4個字元(程式順序從0開始;最大的順序小於4) print(c) # 輸出結果: ll c = s[2:] # 複製第3個字元到最後 print(c) # 輸出結果: llo World! c = s[:4] # 複製第1個字元到第4個 print(c) # 輸出結果: Hell c = s[:] # 複製全部 print(c) # 輸出結果: Hello World!

常見功能

s = ' Hi, I am Tom ' print(s.upper()) # 全部字元轉大寫 # 輸出結果: HI, I AM TOM print(s.lower()) # 全部字元轉小寫 # 輸出結果: hi, i am tom print(s.capitalize()) # 限定開頭大寫 # 輸出結果: Hi, i am tom print(s.title()) # 單字開頭轉大寫 # 輸出結果: Hi, I Am Tom print(len(s)) # 統計字元數 # 輸出結果: 12 print(s. strip()) #清除字串的前後空白 # 輸出結果: Hi, I am Tom