python - 试图用固定在点的“_”反转字符串
问题描述
def r(s):
str = []
for i in len(s):
if (s[i]=='_'):
str = s[i] + str
continue
str = s[i] + str
return str
我尝试使用上面的代码来转换以下字符串
输入: ab_cde
预期输出: ed_cba
解决方案
s = 'ab_cde'
out = ''
for a, b in zip(s, s[::-1]):
if b != '_' and a != '_':
out += b
else:
out += a
print(out)
印刷:
ed_cba
编辑:更多固定点:
s = 'ab_cde_f_ghijk_l'
i, out = iter(ch for ch in s[::-1] if ch != '_'), ''
out = ''.join(ch if ch == '_' else next(i) for ch in s)
print(out)
印刷:
lk_jih_g_fedcb_a
推荐阅读
- java - 无法使用 JDK16.0.2、JavaEE8.0.1、TomCat9.0.52、Vaadin14.6.8 使 helloworld 类型的程序工作
- mysql - 确认我写的查询是否适合 mysql
- python - Python:使用文件列表搜索 XPath
- java - 使用java检索xml节点的值(根据特定属性)
- html - JwtAuthentication 标头不包含任何内容
- excel - 使用 vb.net 更改 excel 单元格颜色
- css - 当我用图像标签替换背景图像时 CSS 网格损坏
- google-cloud-platform - AI Notebook 的 Jupyterlab 运行状况检查失败
- sqlalchemy - SQLAlchemy NoSuchModuleError:无法加载插件:sqlalchemy.dialects:spanner
- c# - ASP.NET Core:UseRequestLocalization 为什么我们需要指定支持的文化列表?