如何在Python中将字符串转换为具有相等间隔的列表? [英] How to convert a string to list with equal interval in Python ?

查看:265
本文介绍了如何在Python中将字符串转换为具有相等间隔的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

x ='abcdefghijklmnopqrstuvwx'

其中,输入(x)='str'

如何获得这个输出

x_list:['abc','def','ghi','jkl','mno','pqr',' stu','vwx']

where,type(x_list)='list',x_list的元素类型为'str'



我尝试过:



我无法获得所需的输出。

x='abcdefghijklmnopqrstuvwx'
where,type(x)='str'
How to get this output:
x_list:['abc','def','ghi','jkl','mno','pqr','stu','vwx']
where, type(x_list)='list' and elements of x_list are of type 'str'

What I have tried:

I am not able to get the desired output.

推荐答案

x='abcdefghijklmnopqrstuvwx'
xlist=[]
for i in range(0,len(x),3):
  xlist.append(x[i:i+3])
print(xlist)


这篇关于如何在Python中将字符串转换为具有相等间隔的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆