How do I slice a string every 3 indices? [duplicat

2019-02-06 14:32发布

This question already has an answer here:

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?

4条回答
SAY GOODBYE
2楼-- · 2019-02-06 15:14

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.

查看更多
爷的心禁止访问
3楼-- · 2019-02-06 15:15

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

查看更多
唯我独甜
4楼-- · 2019-02-06 15:20

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']
查看更多
ら.Afraid
5楼-- · 2019-02-06 15:32

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']
查看更多
登录 后发表回答