python 内置函数
1,split()函数:
Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串
str.split(str="", num=string.count(str))
- str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
- num -- 分割次数。默认为 -1, 即分隔所有。
- 返回分割后的字符串列表。
- shilistr = "Line1-abcdef \nLine2-abc \nLine4-abcd";
- print str.split( ); # 以空格为分隔符,包含 \n -------->['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
- print str.split(' ', 1 ); # 以空格为分隔符,分隔成两个 ----------->['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
2,sort(self, cmp=None, key=None, reverse=False) #key=函数
list类型的排序,sort()没有返回值,对操作原始数据,改变了原始数据,
eg: s = [11,23,4,41,0,2]
s.sort() #s = [0, 2, 4, 11, 23, 41]
s.sort(reverse=True) #s=[41, 23, 11, 4, 2, 0]
3,sorted(iterable, cmp=None, key=None, reverse=False):
iterable --->要迭代的对象
1, sorted("This is a test string from Andrew".split(), key=str.lower) #加了key,忽略大小写 #
-------->['a', 'Andrew', 'from', 'is', 'string', 'test', 'This'] #key=len按照长度进行排序
2,student_tuples = [('john', 'A', 15),('jane', 'B', 12),('dave', 'B', 10),] sorted(student_tuples, key=lambda student: student[2]) # sort by age
-------->[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
3, student_tuples.sort(key=lambda x: x[2])
-------->[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]