python基礎資料型別的相關知識點
阿新 • • 發佈:2018-11-03
1、字串的函式join
>>> s = "Hello" >>> s1 = s.join("你好")#將字串Hello插入到你好中 >>> s1 '你Hello好' >>> s2 = "Tanxu".join("你好嗎")#將字串Tanxu插入到你好嗎中 >>> s2 '你Tanxu好Tanxu嗎'
join可以把列表變成字串
>>> s3 = "_".join(["Tanxu","is","a","good","student"]) >>> s3 'Tanxu_is_a_good_student'
2、list在迴圈的時候不能刪,因為會改變索引
>>> lst = ["Tanxu","is","a","good","student"] >>> for el in lst: lst.remove(el) >>> lst ['is', 'good']
要刪除一個列表:
lst = ["Tanxu","is","a","good","student"] #準備一個空列表 del_lst = [] for el in lst: del_lst.append(el) #記錄下來要刪除的內容 for el in del_lst: #迴圈記錄的內容 lst.remove(el)#刪除原來的內容 print(lst)
#刪除以周開頭的人 lst1 = ["周杰倫","周星馳","周潤發","馬化騰","馬雲"] del_lst1 = [] for el in lst1: del_lst1.append(el) for el in del_lst1: if "周" in el: lst1.remove(el) print(lst1)
3、fromkeys用法:【不會對原來的字典產生影響】
>>> dic = {'a':'123'} >>> s = dic.fromkeys("Hello","你好") #返回一個新的字典 >>> s {'H': '你好', 'e': '你好', 'l': '你好', 'o': '你好'}
4、型別轉換
元組--》類別 list(tuple)
列表轉換成元組 tuple(list)
list==》str str.join(list)
str==>list str.split()
轉換成False的資料:
0,'',None,[],(),{},set() ==》 False