目录
字符串部分
1 len函数 计算字符串长度
2 join函数
3 os.path.join()函数
4 spilt 分割字符串
5 find() 检测字符串是否包含特定字符
6 count() 计数
7 replace() 字符串替换
8 startwith() 检查字符串是否以某字符串开头
9 endwith()检查字符串是否以某字符串结尾
10 lower() 字符串中所有字母转为小写
11 upper 字符串所有字母大写
12 Center 字符串居中
13 strip() 去掉字符串两边的空白字符或者字符串两边的特殊字符
14 partition() 把字符串分成带有指定字符串的几部分
15 isdigit() 判断字符串中是否只包含数字
16 isalpha() 判断字符串中是否只含字母
常用其他函数
reverse函数 列表逆序排列
sort函数 列表排序
单词排序
二维数组排序
Sorted函数 可迭代对象排序
指定字典的key排序
使用两个关键字进行排序
reduce函数
map函数
对列表中每一个元素都使用func函数 返回一个列表(python2)
通过map可以实现类型转换
strftime函数 实现时间\日期数据的格式化
字符串部分
1 len函数 计算字符串长度
不区分数据类型 所有字符按一个字符计算
a = "张asd32334d"
print(len(a))
# 计算字符串所占的字节数 utf-8下汉字占三个字节
print(len(a.encode()))
end:10,12
2 join函数
将seq中每两个相邻元素中间插入字符串str。返回形成新的字符串。
语法: 'str'.join(seq)
参数说明
str:分隔符。可以为空
seq:要连接的元素序列:字符串、元组、字典
上面的语法即:以str作为分隔符,将seq**所有的元素**合并成一个新的字符串
返回值:返回一个以分隔符sep连接各个元素后生成的字符串
a = "张asd32334d"
b = "-".join(a)
print("b:", b)
c = ["a", "b", "c", "d"]
d = "!".join(c)
print("d:", d)
# 字典只取key值进行拼接
dic1 = {'hello': 1, 'good': 2, 'boy': 3, 'doiido': 4}
e = "=".join(dic1)
print("e:", e)
3 os.path.join()函数
语法: os.path.join(path1[,path2[,......]])
返回值:将多个路径组合后返回
注:第一个绝对路径之前的参数将被忽略
os.path.join( '/hello/' , 'good/boy/' , 'doiido' )
end:
'/hello/good/boy/doiido'
4 spilt 分割字符串
把一个字符串按指定的分隔符(必须是字符串中存在的)分割为字符串列表。
语法格式 :
str.split(sep,maxsplit)
参数说明:
str:表示要分割的字符串
sep:指定的分割符
maxsplit:用于指定分割的次数(大于分隔符个数,则默认分割分隔符的个数次)
string1 = "www,gziscas,com,cn"
print(string1.split(',', maxsplit=5))
string2 = "hsadghjf,fdsk,fiorj"
print(string2.split(",", maxsplit=1))
end:
['www', 'gziscas', 'com', 'cn']
['hsadghjf', 'fdsk,fiorj']
5 find() 检测字符串是否包含特定字符
如果包含,则返回开始的索引;否则,返回-1。
示例:
# find()函数
str1 = 'hello world'
# 'wo'在字符串中
print(str1.find('wo'))
# 'wc'不在字符串中
print(str1.find('wc'))
end:6,-1
6 count() 计数
返回str1在string指定索引范围内[start,end)出现的次数。
# count()函数
str1 = 'hello world'
# 统计str中全部字母l的个数
print(str1.count('l'))
# 统计str中从第5+1个字母到最后一个字母中,字母l的个数
print(str1.count('l', 5, len(str1)))
## 输出:
## 3
## 1
7 replace() 字符串替换
将str1中的str2替换成str3,如果指定count,则不超过count次;
语法:
str1.replace(str2, str3,count)
str1 = 'hello world,hello son'
print(str1.replace("hell", "hi"))
# 从左至右只替换1次
print(str1.replace("hell", "hi", 1))
end:
replace在excel表中的应用
8 startwith() 检查字符串是否以某字符串开头
若是,则返回True;否则,返回False;
## startswith()函数
str8 = "Hello Walt Smith"
print(str8.startswith("Hello"))
## 输出:
## True
9 endwith()检查字符串是否以某字符串结尾
若是,则返回True;否则,返回False;
## startswith()函数
str8 = "Hello Walt Smith"
print(str8.endswith("Hello"))
10 lower() 字符串中所有字母转为小写
# lower()函数
str10 = "Hello Walt Smith"
print(str10.lower())
end:
hello walt smith
11 upper 字符串所有字母大写
str10 = "Hello Walt Smith"
print(str10.upper())
end:
"HELLO WALT SMITH"
12 Center 字符串居中
## center()函数
str12 = "Hello Walt Smith"
print(str12.center(20))
print("st12的原长度为%d" % (len(str12)))
print("str12处理后的长度为%d" % (len(str12.center(20))))
13 strip() 去掉字符串两边的空白字符或者字符串两边的特殊字符
str12 = " Hello Walt Smith "
print(str12.strip())
c = ",,,,a,b,c,,,,,,,,,,,b"
print(c.strip("b"))
14 partition() 把字符串分成带有指定字符串的几部分
## partition()函数
str14 = "Are you believe in yourself?"
# "yourself"在字符串中
print(str14.partition("yourself"))
# "you"在字符串中有两个
print(str14.partition("you"))
# "walt"不在字符串中
print(str14.partition("walt"))
## 输出:
## ('Are you believe in ', 'yourself', '?')
## ('Are ', 'you', ' believe in yourself?')
## ('Are you believe in yourself?', '', '')
15 isdigit() 判断字符串中是否只包含数字
是 返回True 否则 返回False
str12 = "3123"
print(str12.isalnum())
end:True
16 isalpha() 判断字符串中是否只含字母
str12 = "asdasf"
print(str12.isalpha())
是 返回True 否则 返回False
常用其他函数
reverse函数 列表逆序排列
a = [12, 34, 55, 3, 4, 2, ]
a.reverse()
print(a)
sort函数 列表排序
列表 的字母(大写往前排,小写在大写后面)和数字排序,字母和数字不能一起排序
a = [12, 34, 55, 3, 4, 2, ]
a.sort(reverse=True)
print(a)
a.sort()
print(a)
# 是字母就只排序字母,不能数字和字母一起排序
b = ["a", "c", "d", "b", "B", "A"]
b.sort()
print(b)
单词排序
比较完首字母,接着比较第二个字母排序
L = ['cat', 'binary', 'big', 'dog']
L.sort()
print(L)
二维数组排序
# 按学生年龄排序
student = [['Tom', 'A', 20], ['Jack', 'C', 18], ['Andy', 'B', 11]]
student.sort(key=lambda student: student[2])
print(student)
python3 的sort函数 只支持key,不再支持cmp
Sorted函数 可迭代对象排序
sort函数只支持列表 但是sorted函数支持所有可迭代对象,
但是sort函数原列表不变,sorted函数会返回一个排完序的新列表。
a = ("a", "b", "d", "c", "dog")
b = list(a)
print(sorted(a))
print(sorted(b))
指定字典的key排序
此时lambda函数用于指定对列表中所有元素进行排序的准则 此处也就是根据字典的key 即字典的age值来排序
此处个人理解就是 sorted函数对列表的每项元素排列,使用lambda来指定用来列表元素的排序的值。
d = [{"name": 'tom', "age": 1}, {"name": "jack", "age": 15}, {"name": "ming", "age": 4}]
d = sorted(d, key=lambda e: e['age'], reverse=True)
print(d)
使用两个关键字进行排序
先根据第一个关键字排序,再根据第二个关键字排序
d = [{"name": 'a', "age": 1}, {"name": "b", "age": 8}, {"name": "c", "age": 4}]
# 先根据第一个关键字排序,再根据第二个关键字排序
d = sorted(d, key=lambda a: (a["age"], a["name"]), )
print(d)
d1 = sorted(d, key=lambda a: (a["name"], a["age"]), )
print(d1)
d = [["a", 2], ["b", 1]]
# 先根据第一个关键字排序,再根据第二个关键字排序
d = sorted(d, key=lambda x: (x[0], x[1]), )
print(d)
# 此处 x 代表了列表中的单个元素,
d = sorted(d, key=lambda x: (x[1], x[0]), )
print(d)
reduce函数
reduce函数是python内置的高级函数
语法 reduce(func,list)
作用是对list的每个元素反复调用函数func,并返回最终结果值
from functools import reduce
a = [1, 2, 3, 4, 56, 78]
def sum1(x, y):
return x + y
print(reduce(sum1, a))
print(reduce(lambda x, y: x + y, a))
map函数
map函数的语法map(func,list)
对列表中每一个元素都使用func函数 返回一个列表(python2)
python3返回的是一个map对象,需用list转化
from functools import reduce
a = [1, 2, 3, 4, 56, 78]
b = [234, 3, 4, 3, 45, 5]
C = map(lambda x, y: x + y, a, b)
print(list(C))
map(lambda x: x + 1, a)
通过lambda函数 使返回值为元组
from functools import reduce
a = [1, 2, 3, 4, 6, 8]
b = [4, 3, 4, 3, 5, 5]
C = map(lambda x, y: (x ** y, x + y), a, b)
print(list(C))
当不传入func时,map就相当于zip 将多个列表相同位置的元素归并到一个元组(python3必须传func)
通过map可以实现类型转换
元组转列表
map(int,(1,2,3))
# 结果如下:
[1,2,3]
字符串转列表
print(list(map(int, "1234")))
提取字典中的key,置于一个列表中
print(list(map(int, {1: 2, 2: 3, 3: 4})))
print(list({1: 2, 2: 3, 3: 4}.keys()))
strftime函数 实现时间\日期数据的格式化
(将任意格式的日期字符串按要求进行格式化) 可把时间转为星期几 一年中的第几天等
strfrime链接
更多推荐
常用数据处理函数
发布评论