tags: python,資料型別

Python 資料型別

最近更新日期:20201013
Python的五種基礎資料型別: 視為資料儲存容器

  • Number(數字): 有int(有號整數),long(沒用過),float(浮點數),complex(複數:例如5.2-2.5i 沒用過)
  • String(字串): 使用'或是" 包括起來的一串字元。
  • List(串列)
  • Tuple(元組)
  • Dictionary(字典)

記憶順序為序對(tuple)串列[list]{字典:dict}集合{set} 四種。

記憶方法: 由小到大 ()-> []-> {},最小的不能變更。

(序對,tuple)

tuple用於依序儲存資料,可以依照順序取出資料,但不能更改,是不可變的物件;

[串列,清單,list]

串列(list)用於依序儲存資料,可以依照順序取出,也可以更改。

用法:

myList = [1,2,'A','B'] #建立List(索引由0開始),1,2,'A','B' 為元素。 print(myList[2]) # A :用「[索引值]」讀取個別元素 # 用for迴圈,取出List的元素 for x in myList: print(x,type(x)) # 1 <class 'int'> # 2 <class 'int'> # A <class 'str'> # B <class 'str'> myList.count('A') # 回傳數值為 'A' 在 myList 中所出現的次數。 # 延伸引用: # 可以用此來判斷 元素是否存在於該list 中。 # (也可用下方index,但是若不存在在list中,會出現錯誤 ck = myList.count('C') # 不存在在myList中,則ck為0 index=myList.index('A') # 用「函式index」取出指定元素的索引值。 # list.index(x[, start[, end]]) 找不到,會丟出 ValueError 錯誤。 print('index=', index) # 2 print(len(myList)) # 4 用「len函式」計算List有多少元素。 List長度。 myList.append('D') # 追加資料。加入List資料。 print(myList) # [1, 2, 'A', 'B', 'D'] myList.insert(4,'C') # 用「函式insert」將元素插入到List的指定位置(第4索引的前面,第4個位置後面)。 print(myList) # [1, 2, 'A', 'B', 'C', 'D'] myList.extend(['a','b']) #將 iterable(可列舉物件)接到 list 的尾端。等同於 a[len(a):] = iterable。 print(myList) # [1, 2, 'A', 'B', 'a', 'b'] myList[len(myList):] = ['c','d'] # 串接list 方法二 print(myList) # [1, 2, 'A', 'B', 'a', 'b', 'c', 'd'] myList.remove('D') # 用「函式remove」將指定的元素從List中刪除 print(myList) # [1, 2, 'A', 'B', 'C'] del myList[3] # 用「函式del」將List中第幾個元素刪除。 print(myList) # [1, 2, 'A', 'C'] myList.clear() # 刪除 list 中所有項目。這等同於 del a[:] # 用「函式pop」將List中第幾個元素刪除,若不指定元素則刪除最後一個元素(採先進後出法)。 myList.pop(0) print('pop(0)',myList) # [2, 'A', 'C'] 有指定,由指定的位置刪除 myList.pop() print('pop()',myList) # [2, 'A'] 沒指定,由最後一個刪除。 myList.pop(-1) print('pop(-1)',myList) # [2] 負數,由右往左刪除。 # 排序:List中資料有數字與字串時,無法排序。(有無其他方法?) myList = ['C','D','Ab','Aa','Bb','Ba','AA','BB','A','B'] myList.sort() print(myList) # ['A', 'AA', 'Aa', 'Ab', 'B', 'BB', 'Ba', 'Bb', 'C', 'D'] myList = [1,2.3,2,8,9,10] myList.sort() print(myList) # [1, 2, 2.3, 8, 9, 10] # 反轉List (將原List反順序排列) myList = ['A','B',2,1] myList.reverse() # [1, 2, 'B', 'A'] print(myList) myList = [1,2.3,2,8,9,10] myList.reverse() print(myList) # [10, 9, 8, 2, 2.3, 1] # List轉為字串列方法:['a','b','c','d'] =>"a,b,c,d" # 方法一:需要元素均為字串,才有辦法使用此法 List=['a','b','c','d'] List2Str ='"'+','.join(List)+'"' print(List2Str) #"a,b,c,d" # 方法二:適用 文數字混和的List List=[1,'A','B',3] List2Str=str(List).replace('[','"',1).rstrip(']')+'"' print(List2Str) # "1, 'A', 'B', 3" List2Str='"'+str(List).lstrip('[').rstrip(']')+'"' print(List2Str) # "1, 'A', 'B', 3"

參考:Python 教學-5.資料結構

{dict:字典} {key : value}

字典儲存資料方式為{鍵(key):值(value)}的對應方式。取出資料方式可使用「鍵」查詢「值」。
字典語法:
dic = {key1 : value1, key2 : value2 }
範例:
d1={} #空字典

運用:

# 建立空字典方法: emptyDict = dict() emptyDict = {} # 建立字典,例如學生考試分數: studentScoreDict = {'Peter':80, 'Lowina':90, 'Alice':85} # 方法一 studentScoreDict = dict(Peter=80, Lowina=90, Alice=85) # 方法二 employeeDict = {83022:'Peter',10011:'Lowina'} # Key 也可以是數字 # 讀取資料方法 myDict = {'Peter':80, 'Lowina':90, 'Alice':85} print(myDict['Peter']) #80 若找不到值,會發生錯誤 print(myDict.get('Lowina')) #90 若找不到值,會傳回None # 新增或是修改 myDict['Peter']=70 # 更新資料 print(myDict['Peter']) # 70 myDict.update({'Peter':60}) # 字典內有,就更新 myDict.update({'Bossin':95}) # 字典內沒有,就新增 print(myDict) # {'Peter': 60, 'Lowina': 90, 'Alice': 85, 'Bossin': 95} myDictLen = len(myDict) # 字典項目數量(長度) del myDict('Bossin') # 刪除字典項目 del myDict # 刪除整個字典 # 字典迴圈處理範例 myDict = {'Peter':80, 'Lowina':90, 'Alice':85} for name in myDict: myDict[name] -= 10 print(myDict) # {'Peter': 70, 'Lowina': 80, 'Alice': 75} 每一位都減10分 # 用.items() 方法,每次迴圈回覆一個字組: myDict = {'Peter':80, 'Lowina':90, 'Alice':85} for name,value in myDict.items(): print(f'{name}:{value}, ', end='') # Peter:80, Lowina:90, Alice:85, for key in dict: print(key) # 列出key值 for key, value in dict.items(): print(key, value) #列出key與資料

dict.clear() # 清空字典所有條目
del dict # 删除字典
參考:第 12 章 字典

{集合}

python的set集合,是一個無序不重複的元素集合。

ls =[1,2,1,3,'A','B','C','A'] ls2 = [1,3,4,5,'d','A',3,4,6] print(type(ls),len(ls),ls) # <class 'list'> 8 [1, 2, 1, 3, 'A', 'B', 'C', 'A'] st = set(ls) # 轉成集合 print(type(st),len(st),st) # <class 'set'> 6 {1, 2, 'B', 3, 'C', 'A'} st2 = set (ls2) # 轉成集合 print(type(st2),len(st2),st2) # <class 'set'> 7 {'A', 1, 3, 4, 5, 6, 'd'} print('交集',st & st2) # 交集 {'A', 1, 3} print('聯集',st | st2) # 聯集 {'A', 1, 2, 3, 'C', 4, 5, 6, 'd', 'B'} print('差集',st - st2) # 差集 {2, 'C', 'B'} 在st中,不在st2 中 print('差集',st2 - st ) # 差集 {'d', 4, 5, 6} 在st2中,不在st 中

參考:python 集合比較(交集、並集,差集)


參考:Python 資料容器

S20200609 By YTC
M20200730,M20200814,M20200919,M20200929,M20201006
M20201013

Select a repo