首页 > 代码库 > python的几个常用内置函数
python的几个常用内置函数
dir()查看属性(函数和数据对象)
help()查看具体的帮助文档
id() 用来查看数据对象的地址
split 分隔(str ---> list):
>>> s="my:name:is:xiaofan"
>>> s.split(":")
[‘my‘, ‘name‘, ‘is‘, ‘xiaofan‘]
join合并(list--->str)
>>> l1=["a","b","c"]
>>> l2=‘-‘.join(l1)
>>> print(l2)
a-b-c
去除(左右两边)空格、制表符等:
>>> name = " xaio fan "
>>> name.strip(‘ ‘)
‘xaio fan‘
>>> name.rstrip(‘ ‘)
‘ xaio fan‘
>>> name.lstrip(‘ ‘)
‘xaio fan ‘
replace替换:
>>> name
‘ xaio fan ‘
>>> name.replace(‘ ‘,‘‘)
‘xaiofan‘
count 计算出现的个数:
>>> name
‘ xaio fan ‘
>>> name.count(‘x‘)
1
find、index 查找字符的索引的位置(区别:index查找不到会报错)
>>> name
‘ xaio fan ‘
>>> name.find(‘i‘)
3
>>> name="this is a boy"
>>> name.find(‘a‘)
8
>>> name.index(‘a‘)
8
>>> name.find(‘e‘)
-1
>>> name.index(‘e‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
zip 合并列表:
>>> a=[1,2,3]
>>> b=[1,2,3]
>>> zip(a,b)
[(1, 1), (2, 2), (3, 3)]
>>> dict(zip(a,b))
{1: 1, 2: 2, 3: 3}
列表追加
>>> list1=[]
>>> list1.append("a")
>>> list1
[‘a‘]
>>> list1.append("b")
>>> list1
[‘a‘, ‘b‘]
>>> list1.extend("c")
>>> list1
[‘a‘, ‘b‘, ‘c‘]
>>> list1.extend(["d","e","f"])
>>> list1
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘]
python的几个常用内置函数