python - 如何按索引添加两个字符串
问题描述
我尝试了很多次,但做不到。
这是一个例子:
print( concat_corresponding( "12345", "6789XYZ" ) )
期望的输出:
162738495XYZ
解决方案
这是一种方法itertools
:
from itertools import chain, zip_longest
a = "12345"
b = "6789XYZ"
res = ''.join(list(chain.from_iterable(zip_longest(a, b, fillvalue=''))))
# '162738495XYZ'
请注意,列表转换不是必需的,但可以提高性能。
推荐阅读
- angular - 如何以反应形式为对象数组声明 FormArray
- android - 如何使BottomSheetDialog全屏?
- node.js - Angular Typescript 数组仅在 array.push() 上添加最后一个元素
- react-native - react native如何将项目版本0.61.0升级到最新版本
- android-studio - E/RecyclerView:没有附加适配器;跳过布局 / Kotlin
- python - 一次导入多个自定义模块
- android - 它的名称是什么以及如何在 xml 中对其进行编码?
- intellij-plugin - 行为类似于断点的 IntelliJ 行标记?
- windows - Windows Makefile 和 TEXINPUTS
- python-3.x - Python3 argparse nargs="+" 获取参数数量