Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/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_Python 2.7 - Fatal编程技术网

Python “字符串”中较小的字符串

Python “字符串”中较小的字符串,python,python-2.7,Python,Python 2.7,我有一根绳子 str = '788399902432345678901234567890' 目前, str[1] = 7 str[2] = 8 等等 我需要重新安排,以便 str[1] = 7883 str[2] = 9990 等等 我试过这个 n = 4 listo = strq split_list = [listo[i:i+n] for i in range(0, len(listo), n)] 但这只是拆分字符串,而不是创建小字符串 请帮我做这个 这应该很好,不是吗?我将变量名从

我有一根绳子

str = '788399902432345678901234567890'
目前,

str[1] = 7
str[2] = 8
等等

我需要重新安排,以便

str[1] = 7883
str[2] = 9990
等等

我试过这个

n = 4
listo = strq
split_list = [listo[i:i+n] for i in range(0, len(listo), n)]
但这只是拆分字符串,而不是创建小字符串


请帮我做这个

这应该很好,不是吗?我将变量名从str改为val,因为str是一个关键字,应该避免将其用作变量名

>>> val='788399902432345678901234567890'
>>> n = 4
>>> val= [val[i:i+n] for i in range(0, len(val), n)]
>>> val[0]
'7883'
>>>

看起来输出正是您想要的

应该有什么不同

strq = '788399902432345678901234567890'
n = 4
listo = strq
split_list = [listo[i:i+n] for i in range(0, len(listo), n)]

['7883', '9990', '2432', '3456', '7890', '1234', '5678', '90']

split_list[0] = '7883'
split_list[1] = '9990'

etc

那么您希望输出结果是什么?拆分列表[0]将是第一组4,以此类推。。。。隐藏内置str也不是一个好主意-称之为文本或数据或其他东西…您编写str[1]=7。但是在python中,索引从0开始,所以最好编写str[0]=7。。。