python基础数据类型的相关知识点
生活随笔
收集整理的這篇文章主要介紹了
python基础数据类型的相关知识点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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
轉載于:https://www.cnblogs.com/tanxu05/p/9902109.html
總結
以上是生活随笔為你收集整理的python基础数据类型的相关知识点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SCU 4439 Vertex Cove
- 下一篇: Anaconda管理多版本的python