python - python中的列表理解空间删除
问题描述
我仍然对python中的列表理解有所了解,但我相信这是我完成这项任务所需要的。
我有一个字符串,我已将其转换为列表。如果两个相邻元素都是小写字母字符,我想删除空格。
例如
INPUT> Bartho lemew The Rhinoceros
OUTPUT> Bartholemew The Rhinoceros
解决方案
我认为re.sub
这里更适合:
import re
def remove_spaces(string):
return re.sub(r'(?<=[a-z]) (?=[a-z])', '', string)
print(remove_spaces('Bartho lemew The Rhinoceros'))
# Bartholemew The Rhinoceros
推荐阅读
- c - 将大数作为单独的数字存储在数组中
- php - 从数据库搜索时如何避免空白的第一个结果
- r - 如何转义 RODBC 连接字符串密码中的特殊字符
- r - 无法在 SQLite 的列中保存多个值
- c++ - 实现自定义异步任务类型并等待
- reactjs - 如何自定义 Highcharts.js 中的条形宽度和条形间距
- javascript - WebAudio API:如何在 AudioWorkletProcessor 中访问时间/采样率?
- chart.js - 无法在图表 js 注释插件中旋转标签
- javascript - 节点js中的击键输入键发送
- c# - 错误:“System.InvalidOperationException:无法找到所需的服务。”如何解决?