每第n个字符在起始字符上迭代时分割python字符串 [英] Split python string every nth character iterating over starting character
本文介绍了每第n个字符在起始字符上迭代时分割python字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找到一种优雅的方法来在每n个字符中分割一个python字符串,并迭代以哪个字符开头.
I'm trying to find an elegant way to split a python string every nth character, iterating over which character to start with.
例如,假设我有一个包含以下内容的字符串:
For example, suppose I have a string containing the following:
ANDTLGY
我想将字符串分成3个字符,如下所示:
I want to split the string into a set of 3 characters looking like this:
['AND','NDT','DTL','TLG','LGY']
推荐答案
a='ANDTLGY'
def nlength_parts(a,n):
return map(''.join,zip(*[a[i:] for i in range(n)]))
print nlength_parts(a,3)
希望您可以向教授解释它是如何工作的;)
hopefully you can explain to the professor how it works ;)
这篇关于每第n个字符在起始字符上迭代时分割python字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文