python - 您好,我的任务是编写函数并通过测试。我只能通过一项测试,有人可以帮助我完成第二项测试吗?
问题描述
我的任务是编写函数 def myreplace(old, new, s): 并通过测试。我可以通过第一个测试,但我不能通过第二个测试,因为有很多空格。有没有办法做到这一点?
def myreplace(old, new, s):
# Replace all occurrences of old with new in s...
new_s = ""
for i in s.split():
s.split(old)
new_s = new.join(s.split(old))
return new_s
test(myreplace(",", ";", "this, that, and some other thing") ==
"this; that; and some other thing")
test(myreplace(" ", "**",
"Words will now be separated by stars.") ==
"Words**will**now**be**separated**by**stars.")
解决方案
这个怎么样:
def myreplace(old, new, s):
# Replace all occurrences of old with new in s...
if old == ' ':
return new.join(s.split())
else:
return new.join(s.split(old))
推荐阅读
- algorithm - 将“并行性”引入任务调度问题
- php - Symfony 4 表单 VichImageType 更新自动填充
- python - 如何在 python selenium 中设置 chrome 设置?
- windows - 如何一次在多个 Windows 服务器上运行 bat 脚本?
- syntax - 为什么在模式匹配中使用“ref”而不是星号?
- java - 从 json 动态填充微调器数据
- google-cloud-platform - 为什么我们看到 memorystore 从 700 条记录中提取一条记录需要更多时间?
- c# - 依赖程序集仅在被代码引用时部署
- terraform - Terraform:自动填充模块的变量
- python - 我正在用python编写下面提到的简单代码,但它没有打开