it-source

n번째 문자마다 문자열을 분할하시겠습니까?

criticalcode 2023. 1. 15. 16:56
반응형

n번째 문자마다 문자열을 분할하시겠습니까?

n번째 문자마다 문자열을 분할할 수 있습니까?

예를 들어 다음과 같은 문자열이 있다고 가정합니다.

'1234567890'

어떻게 하면 이렇게 만들 수 있을까요?

['12','34','56','78','90']
>>> line = '1234567890'
>>> n = 2
>>> [line[i:i+n] for i in range(0, len(line), n)]
['12', '34', '56', '78', '90']

마지막으로 regex를 사용하여 다음을 수행할 수 있습니다.

>>> import re
>>> re.findall('..','1234567890')
['12', '34', '56', '78', '90']

홀수 문자의 경우 다음과 같이 할 수 있습니다.

>>> import re
>>> re.findall('..?', '123456789')
['12', '34', '56', '78', '9']

또한 다음 작업을 수행하여 긴 청크의 regex를 단순화할 수도 있습니다.

>>> import re
>>> re.findall('.{1,2}', '123456789')
['12', '34', '56', '78', '9']

그리고 당신은 사용할 수 있습니다.re.finditer문자열이 긴 경우 청크별로 청크를 생성합니다.

이를 위한 python에는 이미 내장된 함수가 있습니다.

>>> from textwrap import wrap
>>> s = '1234567890'
>>> wrap(s, 2)
['12', '34', '56', '78', '90']

랩의 docstring은 다음과 같습니다.

>>> help(wrap)
'''
Help on function wrap in module textwrap:

wrap(text, width=70, **kwargs)
    Wrap a single paragraph of text, returning a list of wrapped lines.

    Reformat the single paragraph in 'text' so it fits in lines of no
    more than 'width' columns, and return a list of wrapped lines.  By
    default, tabs in 'text' are expanded with string.expandtabs(), and
    all other whitespace characters (including newline) are converted to
    space.  See TextWrapper class for available keyword args to customize
    wrapping behaviour.
'''

요소를 n개의 길이 그룹으로 그룹화하는 또 다른 일반적인 방법은 다음과 같습니다.

>>> s = '1234567890'
>>> map(''.join, zip(*[iter(s)]*2))
['12', '34', '56', '78', '90']

이 메서드는 의 문서에서 직접 가져옵니다.

이 버전이 itertools 버전보다 짧고 읽기 쉽다고 생각합니다.

def split_by_n(seq, n):
    '''A generator to divide a sequence into chunks of n units.'''
    while seq:
        yield seq[:n]
        seq = seq[n:]

print(list(split_by_n('1234567890', 2)))

Py의 더 많은 도구 사용PI:

>>> from more_itertools import sliced
>>> list(sliced('1234567890', 2))
['12', '34', '56', '78', '90']

이 솔루션이 마음에 듭니다.

s = '1234567890'
o = []
while s:
    o.append(s[:2])
    s = s[2:]

를 사용할 수 있습니다.grouper()에서 만든 요리하다.itertools:

Python 2.x:

from itertools import izip_longest    

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

Python 3.x:

from itertools import zip_longest

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

이러한 기능은 메모리 효율이 뛰어나고 모든 반복 가능 항목에서 작동합니다.

이것은 단순한 for 루프에 의해 실현될 수 있습니다.

a = '1234567890a'
result = []

for i in range(0, len(a), 2):
    result.append(a[i : i + 2])
print(result)

출력은 ['12', '34', '56', '78', '90', 'a'와 같습니다.

나는 같은 장면에 갇혔다.

이건 내게 효과가 있었다.

x="1234567890"
n=2
list=[]
for i in range(0,len(x),n):
    list.append(x[i:i+n])
print(list)

산출량

['12', '34', '56', '78', '90']

다음 코드를 사용해 보십시오.

from itertools import islice

def split_every(n, iterable):
    i = iter(iterable)
    piece = list(islice(i, n))
    while piece:
        yield piece
        piece = list(islice(i, n))

s = '1234567890'
print list(split_every(2, list(s)))

이것을 시험해 보세요.

s='1234567890'
print([s[idx:idx+2] for idx,val in enumerate(s) if idx%2 == 0])

출력:

['12', '34', '56', '78', '90']
>>> from functools import reduce
>>> from operator import add
>>> from itertools import izip
>>> x = iter('1234567890')
>>> [reduce(add, tup) for tup in izip(x, x)]
['12', '34', '56', '78', '90']
>>> x = iter('1234567890')
>>> [reduce(add, tup) for tup in izip(x, x, x)]
['123', '456', '789']

항상 그렇듯이 라이너 하나를 사랑하는 사람들을 위해

n = 2  
line = "this is a line split into n characters"  
line = [line[i * n:i * n+n] for i,blah in enumerate(line[::n])]

more_itertools.sliced이미 언급되어 있습니다.라이브러리의 기타 4가지 옵션은 다음과 같습니다.

s = "1234567890"

["".join(c) for c in mit.grouper(2, s)]

["".join(c) for c in mit.chunked(s, 2)]

["".join(c) for c in mit.windowed(s, 2, step=2)]

["".join(c) for c in  mit.split_after(s, lambda x: int(x) % 2 == 0)]

후자의 각 옵션에 의해, 다음의 출력이 생성됩니다.

['12', '34', '56', '78', '90']

설명되는 옵션 매뉴얼: , , ,

짧은 문자열에 대한 간단한 재귀 솔루션:

def split(s, n):
    if len(s) < n:
        return []
    else:
        return [s[:n]] + split(s[n:], n)

print(split('1234567890', 2))

또는 다음과 같은 형태로:

def split(s, n):
    if len(s) < n:
        return []
    elif len(s) == n:
        return [s]
    else:
        return split(s[:n], n) + split(s[n:], n)

이것은 재귀적 접근법의 전형적인 분할과 정복 패턴을 보다 명확하게 보여준다(실제로 이 방법으로 할 필요는 없지만).

솔루션:groupby:

from itertools import groupby, chain, repeat, cycle

text = "wwworldggggreattecchemggpwwwzaz"
n = 3
c = cycle(chain(repeat(0, n), repeat(1, n)))
res = ["".join(g) for _, g in groupby(text, lambda x: next(c))]
print(res)

출력:

['www', 'orl', 'dgg', 'ggr', 'eat', 'tec', 'che', 'mgg', 'pww', 'wza', 'z']

이 답변들은 모두 훌륭하고 효과가 있지만 구문이 너무 복잡해서...간단한 함수를 쓰면 어떨까요?

def SplitEvery(string, length):
    if len(string) <= length: return [string]        
    sections = len(string) / length
    lines = []
    start = 0;
    for i in range(sections):
        line = string[start:start+length]
        lines.append(line)
        start += length
    return lines

간단히 말하면 다음과 같습니다.

text = '1234567890'
lines = SplitEvery(text, 2)
print(lines)

# output: ['12', '34', '56', '78', '90']

언급URL : https://stackoverflow.com/questions/9475241/split-string-every-nth-character

반응형