开发者

How do I slice a string every 3 indices? [duplicate]

This question already has answers here: How to iterate over a list in chunks (39 ans开发者_运维技巧wers) Closed 7 years ago.

I'm using Python to program for the lab I work at. How can I slice out every 3 characters in a given string and append it to a list?

i.e. XXXxxxXXXxxxXXXxxxXXXxxxXXX (where X or x is any given letter)

string = 'XXXxxxXXXxxxXXXxxxXXXxxxXXX'
mylist = []

for x in string:
    string[?:?:?]
    mylist.append(string)

I want the list to look like this: ['XXX','xxx','XXX','xxx','XXX'....etc]

Any ideas?


In short, you can't.

In longer, you'll need to write your own function, possibly:

def split(str, num):
    return [ str[start:start+num] for start in range(0, len(str), num) ]

For example:

>>> split("xxxXXX", 3)
['xxx', 'XXX']
>>> split("xxxXXXxx", 3)
['xxx', 'XXX', 'xx']


one difference between splitting lists into chunks of 3 and strings into chunks of 3 is that the re module works with strings rather than lists.

If performance is important (ie you are splitting thousands of strings), you should test how the various answers compare in your application

>>> import re
>>> re.findall('...','XXXxxxXXXxxxXXXxxxXXXxxxXXX')
['XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX']

>>> chunksize=3
>>> re.findall('.{%s}'%chunksize,'XXXxxxXXXxxxXXXxxxXXXxxxXXX')
['XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX']

This works because . means "match any character" in regular expressions.
.{3} means "match any 3 characters", and so on


As far as I know there is no built in method that allows you to chunk an str every x indices. However this should works:

 str = "stringStringStringString"

 def chunk_str(str, chunk_size):
   return [str[i:i+chunk_size] for i in range(0, len(str), chunk_size)]

 chunk_str(str,3)

produces:

['str', 'ing', 'Str', 'ing', 'Str', 'ing', 'Str', 'ing']


Copying an answer from How do you split a list into evenly sized chunks in Python? since Nov 2008:

Directly from the Python documentation (recipes for itertools):

from itertools import izip, chain, repeat

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)

An alternate take, as suggested by J.F.Sebastian:

from itertools import izip_longest

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return izip_longest(*[iter(iterable)]*n, fillvalue=padvalue)

I guess Guido's time machine works—worked—will work—will have worked—was working again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜