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