Python 编程骚操作连载(一)- 字符串、列表、字典和集合的处理(Part A)

简介: Python 编程骚操作连载(一)- 字符串、列表、字典和集合的处理(Part A)

一、字符串的处理

拆分含有多种分隔符的字符串

如何对一个含有多种特殊字符的字符串进行分割处理,比如向下面这种字符串

str = "A&man$;*who/stands|for+noting=will-fall,for%anything"
复制代码

如何去除特殊风格符获取字符串中所有单词的列表?

如果字符串中只包含单一分隔符的话,可以使用字符串对象的 split 方法,该方法的第一个参数就是分隔符,默认是空格。

str = "A man who stands for noting will fall for anything"
str_list = str.split()
print(str_list)
复制代码

执行上述代码,输出结果如下:

['A', 'man', 'who', 'stands', 'for', 'noting', 'will', 'fall', 'for', 'anything']
复制代码

针对包含多种分割符的情况下可以连续调用字符串对象的 split 方法,每次处理一种分隔符

str = "A&man$;*who/stands|for+noting=will-fall,for%anything"
# 第一次处理
res = str.split('&')
print(res)
temp = []
list(map(lambda x: temp.extend(x.split('$')), res))
print(temp)
# 第二次处理
res = temp
temp = []
list(map(lambda x: temp.extend(x.split(';')), res))
print(temp)
# 第三次处理
res = temp
temp = []
list(map(lambda x: temp.extend(x.split('*')), res))
print(temp)
复制代码

执行上述代码,输出结果如下:

['A', 'man$;*who/stands|for+noting=will-fall,for%anything']
['A', 'man', ';*who/stands|for+noting=will-fall,for%anything']
['A', 'man', '', '*who/stands|for+noting=will-fall,for%anything']
['A', 'man', '', '', 'who/stands|for+noting=will-fall,for%anything']
复制代码

根据输出结果可以确定,第一次处理是将字符串中的 &,第二次处理掉了 $...。依次类推,可以将代码抽取为一个函数

def split_multi_chars(str, chars):
    res = [str]
    for c in chars:
        t = []
        list(map(lambda x: t.extend(x.split(c)), res))
        res = t
        print(res)
    return res
str_data = "A&man$;*who/stands|for+noting=will-fall,for%anything"
chars = '&$;*/|+=-,%'
print(split_multi_chars(str_data, chars))
复制代码

执行上述代码,输出结果如下:

['A', 'man$;*who/stands|for+noting=will-fall,for%anything']
['A', 'man', ';*who/stands|for+noting=will-fall,for%anything']
['A', 'man', '', '*who/stands|for+noting=will-fall,for%anything']
['A', 'man', '', '', 'who/stands|for+noting=will-fall,for%anything']
['A', 'man', '', '', 'who', 'stands|for+noting=will-fall,for%anything']
['A', 'man', '', '', 'who', 'stands', 'for+noting=will-fall,for%anything']
['A', 'man', '', '', 'who', 'stands', 'for', 'noting=will-fall,for%anything']
['A', 'man', '', '', 'who', 'stands', 'for', 'noting', 'will-fall,for%anything']
['A', 'man', '', '', 'who', 'stands', 'for', 'noting', 'will', 'fall,for%anything']
['A', 'man', '', '', 'who', 'stands', 'for', 'noting', 'will', 'fall', 'for%anything']
['A', 'man', '', '', 'who', 'stands', 'for', 'noting', 'will', 'fall', 'for', 'anything']
['A', 'man', '', '', 'who', 'stands', 'for', 'noting', 'will', 'fall', 'for', 'anything']
复制代码

根据要去除的特殊字符组成的字符串,依次去除了目标字符串中的所有特殊字符,但是最终输出的字符串中包含了空字符串,可以通过列表推导式去除

def split_multi_chars(str, chars):
    # 其余代码保持不变
    # 使用列表推导式去除空字符串
    return list(x for x in t if x)
# 其余代码保持不变
复制代码

再次执行上述代码,输出结果如下:

# 其余输出结果与上次一致
['A', 'man', 'who', 'stands', 'for', 'noting', 'will', 'fall', 'for', 'anything']
复制代码

除了通过连续调用字符串对象的 split 方法依次去除目标字符串中的特殊字符外,还可以通过正则表达式的 split 方法来去除字符串中的特殊字符。

import re
str = "A&man$;*who/stands|for+noting=will-fall,for%anything"
chars = '&$;*/|+=-,%'
res = re.split(r'[=,&$;*/|+%-]+', str)
print(res)
复制代码

执行上述代码,输出结果如下:

['A', 'man', 'who', 'stands', 'for', 'noting', 'will', 'fall', 'for', 'anything']
复制代码

字符串中时间格式的替换

以程序运行过程中生成的日志为例,如果想要替换其中的时间格式应该如何操作?

'Started GET "/students" for 127.0.0.1 at 2022-06-11 01:28:05 +0800'
复制代码

可以使用正则表达式的 sub 方法替换字符串,首先使用正则表达式匹配到时间的内容如年月日等,再调整匹配到的内容的顺序。

import re
log = 'Started GET "/students" for 127.0.0.1 at 2022-06-11 01:28:05 +0800'
res = re.sub('(\d{4})-(\d{2})-(\d{2})', r'\2/\3/\1', log)
# 给每个组起名
res_01 = re.sub('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})', r'\g<month>/\g<day>/\g<year>', log)
print(res)
print(res_01)
复制代码

执行上述代码,输出结果如下:

Started GET "/students" for 127.0.0.1 at 06/11/2022 01:28:05 +0800
Started GET "/students" for 127.0.0.1 at 06/11/2022 01:28:05 +0800
复制代码

时间格式已被成功替换为指定的格式。

字符串的拼接

对于字符串拼接来说最常用的方法就是 + 操作符,起始 + 操作符是调用了 str 对象底层的 __add__ 方法实现的拼接,包括其他的操作符如 >、<、= 等都是调用的底层的以双下划线开头和结尾的方法

str_01 = '234324'
str_02 = 'adcdaf'
print(str_01.__add__(str_02))
print(str.__add__(str_01, str_02))
print(dir(str))
复制代码

执行上述代码,输出结果如下:

37ee32c507c14474be5e4a06244c7147_tplv-k3u1fbpfcp-zoom-in-crop-mark_4536_0_0_0.png

当使用 + 操作符拼接多个字符串时会伴随着大量的字符串的创建和销毁,如果要拼接的字符串非常多,这将会消耗大量的资源,而 str 对象的 join 方法可以更加快速的拼接字符串列表中所有的字符串。

new_str = ' '.join(['A', 'man', 'who', 'stands', 'for', 'noting', 'will', 'fall', 'for', 'anything'])
print(new_str)
复制代码

执行上述代码,输出结果如下:

A man who stands for noting will fall for anything
复制代码

如果要拼接的元素中不只是字符串类型,还包含了其他类型,如数字等,则可以使用生成器表达式,将其他类型元素转换为字符串类型之后再进行拼接。

list_data = ['A', 'man', 'who', 123, 'for', 1345, 'will', 'fall']
new_str = ''.join(str(x) for x in list_data)
print(new_str)
复制代码

执行上述代码,输出结果如下:

Amanwho123for1345willfall
复制代码

推荐使用生成器表达式,不推荐使用列表表达式,列表表达式会返回一个列表


相关文章
|
开发者 Python
Python列表推导式:优雅与效率的完美结合
Python列表推导式:优雅与效率的完美结合
694 116
|
大数据 开发者 Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
585 109
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
692 119
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
|
索引 Python
Python 列表切片赋值教程:掌握 “移花接木” 式列表修改技巧
本文通过生动的“嫁接”比喻,讲解Python列表切片赋值操作。切片可修改原列表内容,实现头部、尾部或中间元素替换,支持不等长赋值,灵活实现列表结构更新。
710 1
|
数据采集 Python
如何从 Python 中的字符串列表中删除特殊字符?
如何从 Python 中的字符串列表中删除特殊字符?
869 0
|
存储 安全 Serverless
Python学习四:流程控制语句(if-else、while、for),高级数据类型(字符串、列表、元组、字典)的操作
这篇文章主要介绍了Python中的流程控制语句(包括if-else、while、for循环)和高级数据类型(字符串、列表、元组、字典)的操作。
767 0
|
Python
Python操作:字符串--列表--元组--字典--运算符 (一)
Python操作:字符串--列表--元组--字典--运算符 (一)
378 0
|
Python
Python操作:字符串--列表--元组--字典--运算符 (二)
Python操作:字符串--列表--元组--字典--运算符 (二)
313 0
|
存储 索引 Python
Python基础 笔记(七) 容器--字符串、列表
Python基础 笔记(七) 容器--字符串、列表
300 4

推荐镜像

更多