如何联合合并切片字符串的各个部分?

How you can unite-merge parts of a sliced string?

有没有办法在 python 中连接合并一个切片字符串,这样您就可以在切片后得到原始字符串?如果不是用什么语言可以实现?
我做了编辑,希望对您有所帮助。

下面有一个文本示例
预切文本的开头:
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
预切文本结束

我用 s[start:stop:step] 分割文本,在这个例子中我把它分成两部分

s = """abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz"""
#part 1 result
print (s[0::2])
#part 2 result
print (s[1::2])


我怎样才能加入结果第 1 部分和第 2 部分,这样我就可以得到原始的预切片文本。
原始预切片文本。
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz

感谢大家的努力。


基于斯蒂芬的第四次回复。

>>> s, t = """acegikmoqsuwy
... bdfhjlnprtvxz""", """bdfhjlnprtvxzacegikmoqsuwy"""
>>> tmp = list(s + t)
>>> tmp[0::2], tmp[1::2] = s, t
>>> print(''.join(tmp))
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz

用它在 3

中分割 PGP 消息
s = """hQEMA1H5MO53CAz0AQf+PoxwzD+9TYqGg61W/ng8Umi+mFPneIC4+JlMu2H4mCsx
AzO5APdXJJHRFjSpS01ZeKQQ05xEeHCnjgmWEFCWUVXW77kd/YI9Q01QIyoo8J6y
b9Wu4uFPQiCy2+Fr4SIKtrpOrsbrqG54BPFY66NeQ9NbG217QCbcaUlXHpmokiIc
6T94uTkLRo+Lv2QWzMBfyfyWbuU2lwD+7dD45oRB7cXInM9jQDHL8FKcFutTxPXh
8w/8eCZtPOQwCAX+vnRFT4DtgDotRdagDEswtuPTd8YbpemIa/C01ylExgIVvtLP
JwimeWx9d+nJgL2nx2emaYzN0vNQStOQceWtYxbbJdJJAXniDi7+HApeHyN2JcKu
LFjdTQFaQB0JGdloIXSJBqhOCMXBfd7mJ5c9sY6abE4bfP9/znbqT35y3GVU9RyX
NVKOujgxzi0/xQ==
=DvXG"""
print (s[0::3])
print (s[1::3])
print (s[2::3])

并加入其中

>>> s, t, d = """hMHOC0foz9q6/8iFe4l2mxzAXHjSZQ5enmFUWkYQQoJ
WuQyFStObGBYN9G7bUHoI64kovWBfb2Dd5BXMQLKuxhwetQAvFDDRgsudbm/1EIt
iWdJ22aNNtctbdAi7AH2KLdFBGoSqCB75sa4Pzq5G9XVux0Q
v""", """QA55AA+xDTG1nU+PI+MHC
OPJRS0eQxHjWCV7dI0Io6buFi2rIrrr5P6eN2QclpkcTuL+2zfyul+Do7I9D8ctP
/CPwXnTtodDwP8pICyxVLJmx+gneY0QOeYbJXD+pyJuFTa0dIJhMfmcYbb9nTyVR
Kjz/==X""", """E1M3zQPw+YgWgmmnCJu4sA5dJFp1K0ECgEWX7/91y8y94PC+4Kpsq4F6Qb1CaXmi
9TRLQMyWUw74RcnjHFFTX88ZOC+R4gtaEtTYea0lgvPwe9nLxmzvSQWxJJniHeNc
jQQJlXBOXdJ96Ef/b33UyNOgix=DG"""
tmp = list(s + t + d)
tmp[0::3], tmp[1::3], tmp[2::3] = s, t, d
print(''.join(tmp))


您收到原始 PGP 消息
hQEMA1H5MO53CAz0AQf+PoxwzD+9TYqGg61W/ng8Umi+mFPneIC4+JlMu2H4mCsx
AzO5APdXJJHRFjSpS01ZeKQQ05xEeHCnjgmWEFCWUVXW77kd/YI9Q01QIyoo8J6y
b9Wu4uFPQiCy2+Fr4SIKtrpOrsbrqG54BPFY66NeQ9NbG217QCbcaUlXHpmokiIc
6T94uTkLRo+Lv2QWzMBfyfyWbuU2lwD+7dD45oRB7cXInM9jQDHL8FKcFutTxPXh
8w/8eCZtPOQwCAX+vnRFT4DtgDotRdagDEswtuPTd8YbpemIa/C01ylExgIVvtLP
JwimeWx9d+nJgL2nx2emaYzN0vNQStOQceWtYxbbJdJJAXniDi7+HApeHyN2JcKu
LFjdTQFaQB0JGdloIXSJBqhOCMXBfd7mJ5c9sY6abE4bfP9/znbqT35y3GVU9RyX
NVKOujgxzi0/xQ==
=DvXG

如果两部分长度相等:

>>> s, t = 'acegikmoqsuwy', 'bdfhjlnprtvxz'
>>> print(''.join(a + b for a, b in zip(s, t)))
abcdefghijklmnopqrstuvwxyz

如果他们不这样做:

>>> s, t = 'acegikmoqsuwy\nbdfhjlnprtvxz', 'bdfhjlnprtvxzacegikmoqsuwy'
>>> from itertools import zip_longest
>>> print(''.join(a + b for a, b in zip_longest(s, t, fillvalue='')))
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz

或者:

>>> s, t = 'acegikmoqsuwy\nbdfhjlnprtvxz', 'bdfhjlnprtvxzacegikmoqsuwy'
>>> tmp = list(s + t)
>>> tmp[0::2], tmp[1::2] = s, t
>>> print(''.join(tmp))
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz

或者:

>>> s, t = 'acegikmoqsuwy\nbdfhjlnprtvxz', 'bdfhjlnprtvxzacegikmoqsuwy'
>>> print(''.join((s, t)[i % 2][i // 2] for i in range(len(s + t))))
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz