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
复制代码

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


相关文章
|
5月前
|
开发者 Python
Python列表推导式:优雅与效率的完美结合
Python列表推导式:优雅与效率的完美结合
498 116
|
5月前
|
大数据 开发者 Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
439 109
|
5月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
381 3
|
5月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
607 3
|
5月前
|
Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
|
5月前
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
418 3
|
5月前
|
索引 Python
Python 列表切片赋值教程:掌握 “移花接木” 式列表修改技巧
本文通过生动的“嫁接”比喻,讲解Python列表切片赋值操作。切片可修改原列表内容,实现头部、尾部或中间元素替换,支持不等长赋值,灵活实现列表结构更新。
243 1
|
5月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
520 0
|
存储 安全 Serverless
Python学习四:流程控制语句(if-else、while、for),高级数据类型(字符串、列表、元组、字典)的操作
这篇文章主要介绍了Python中的流程控制语句(包括if-else、while、for循环)和高级数据类型(字符串、列表、元组、字典)的操作。
414 0
|
Python
Python操作:字符串--列表--元组--字典--运算符 (一)
Python操作:字符串--列表--元组--字典--运算符 (一)
202 0

推荐镜像

更多