string - 如果包含子字符串,则使用替换更改整个字符串
问题描述
a = “thanklynette i owe you one best regardsjohn”
b = [(“lynette”,”john”)]
for i in b:
a.str.replace(b,’’)
print(a)
我收到一个类型错误,因为替换不接受列表。有办法解决吗?我想要的输出是
a = “i owe you one best”
或者
a = “thank i owe you one best regards”
解决方案
我不确定为什么名称在列表中的元组中,如果它不是重要的部分,你可以这样做。您的代码试图a
用列表b
而不是其中的值替换。
a = 'thanklynette i owe you one best regardsjohn'
b = ['lynette','john']
s = a
for i in b:
s = s.replace(i," ")
print(s)
但是,如果元组按照格式很重要,您可以包含一个嵌套循环来访问每个元素。
for i in b:
for j in i:
s = s.replace(j," ")
输出
thank i owe you one best regards
推荐阅读
- c# - 使用电子表格将表格添加到现有的 excel 文件
- android - 以编程方式将文本转换为印刷样式(即文本赃物)
- javascript - 将录制的音频从浏览器发送到 django 服务器并保存为 .wav
- python - 如何从python和rust中存储和读取redis中的常用数据?
- ios - react-native 应用程序在设备中安装时崩溃
- javascript - 材质界面 | 反应 | OnClick 事件总是发送最后一个索引
- java - 将数据从 java 内置类存储到自定义类
- java - 如果我的物体碰到了障碍,我如何在通过障碍并结束比赛时增加分数?
- html - 使用文章元素时 z-index 无法正常工作
- parse-platform - 客户数据库结构