One possible way: join the inner tuples and then the outer list.
(一种可能的方式:加入内部元组,然后加入外部列表。)
''.join(''.join(x) for x in zip(a,b))
Though, zip()
will always stop aggregating when the shortest beween a
and b
ends ( 1a2b3c4d5e
in your example).
(不过,当a
和b
之间的最短端点(在您的示例中为1a2b3c4d5e
)结束时, zip()
将始终停止聚合。)
If you want to reach the end of the longest input string you must iterate them differently, for example: (如果要到达最长输入字符串的末尾,则必须进行不同的迭代,例如:)
c = []
for x in range(max(len(a),len(b))):
c.append(a[x] if x < len(a) else '')
c.append(b[x] if x < len(b) else '')
result=''.join(c)
Or, as suggested by Moinuddin below, using izip_longest
:
(或者,如下面的izip_longest
建议的那样,使用izip_longest
:)
''.join(''.join(x) for x in izip_longest(a, b, fillvalue=''))
NOTE that as of Python 3, izip_longest()
is now zip_longest()
.
(注意 ,从Python 3开始, izip_longest()
现在为zip_longest()
。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…