Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将切片字符串返回到原始字符串_Python_String - Fatal编程技术网

Python 将切片字符串返回到原始字符串

Python 将切片字符串返回到原始字符串,python,string,Python,String,如果我有两条线 s1='abcdefghi' s2='jklmnopqr' 我将第一个字符串s1分为子字符串['abc'、'def'、'ghi'],然后我用函数对其进行编码,该函数为每个子序列提供编号[10,2,33]。 然后我用另一个函数解码,返回[abc','def','ghi'] 对于字符串s2 现在,我想知道解码后如何将子字符串返回到 原始字符串s1='abcdefghi'和s2='jklmnopqr'编辑: 对于更新的问题,您可以使用以下选项: >>> lst =

如果我有两条线

s1='abcdefghi'

s2='jklmnopqr'
我将第一个字符串
s1
分为子字符串
['abc'、'def'、'ghi']
,然后我用函数对其进行编码,该函数为每个子序列提供编号
[10,2,33]
。 然后我用另一个函数解码,返回[abc','def','ghi']

对于字符串
s2

现在,我想知道解码后如何将子字符串返回到 原始字符串
s1='abcdefghi'
s2='jklmnopqr'
编辑:

对于更新的问题,您可以使用以下选项:

>>> lst = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr']
>>> s1, s2 = map("".join, zip(lst[::3], lst[1::3], lst[2::3]))
>>> s1
'abcdefghi'
>>> s2
'jklmnopqr'
>>>
在上面的演示中,
lst
表示函数返回的列表。

您可以使用
join()


Python中的字符串是不可变的。这意味着您不能更改字符串对象。若在赋值中重复使用标识符,则创建一个新的字符串对象

>>> s = 'a'
>>> id(s)
10767896
>>> s = 'b'
>>> id(s)
10767920
>>> decodes = ['abc','def','ghi']
>>> ''.join(decodes)
'abcdefghi'
>>> answer = ''.join(decodes)
>>> print(answer)
abcdefghi
>>> s = 'a'
>>> id(s)
10767896
>>> s = 'b'
>>> id(s)
10767920