python基础数据类型的相关知识点

it2022-05-22  55

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


最新回复(0)