回答(23)
2 years ago
我认为这比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))
2 years ago
我有这个代码,我需要这样做时使用:
def split_string(n, st):
lst = [""]
for i in str(st):
l = len(lst) - 1
if len(lst[l]) < n:
lst[l] += i
else:
lst += [i]
return lst
print(split_string(3, "test_string."))
哪里:
n 是每个列表项的长度
st 是要拆分的字符串
lst 是 st 的列表版本
i 是 st 中使用的当前字符
l 是最后一个列表项的长度
2 years ago
def split(s, n):
"""
Split string every nth character
Parameters
----------
s: string
n: value of nth
"""
new_list = []
for i in range(0, len(s), n):
new_list.append(s[i:i+n])
return new_list
print(split('1234567890', 2))
2 years ago
将元素分组为n长度组的另一种常用方法:
>>> s = '1234567890'
>>> map(''.join, zip(*[iter(s)]*2))
['12', '34', '56', '78', '90']
此方法直接来自zip()的文档 .
2 years ago
>>> 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.
'''
2 years ago
使用itertools . 本手册的"Recipes"部分提供了完成此操作的功能:
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)
2 years ago
我喜欢这个解决方案:
s = '1234567890'
o = []
while s:
o.append(s[:2])
s = s[2:]
2 years ago
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']
2 years ago
短字符串的简单递归解决方案:
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)
,它更明确地说明了递归方法中典型的分而治之模式(尽管实际上没有必要这样做)
2 years ago
这是针对更一般情况的另一种解决方案,其中块的长度不相等 . 如果长度为0,则返回所有剩余部分 .
data 是要拆分的序列; fieldsize 是一个带有字段长度列表的元组 .
def fieldsplit(data=None, fieldsize=()):
tmpl=[];
for pp in fieldsize:
if(pp>0):
tmpl.append(line[:pp]);
line=line[pp:];
else:
tmpl.append(line);
break;
return tuple(tmpl);
2 years ago
一如往常,对于那些喜欢一个衬垫的人
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])]
2 years ago
我用这个:
list(''.join(s) for s in zip(my_str[::2], my_str[1::2]))
或者您可以使用任何其他 n 数字而不是 2 .
2 years ago
试试这个:
s='1234567890'
print([s[idx:idx+2] for idx,val in enumerate(s) if idx%2 == 0])
输出:
['12', '34', '56', '78', '90']
2 years ago
幽灵般的 - 试图发明另一个答案:
def split(s, chunk_size):
a = zip(*[s[i::chunk_size] for i in range(chunk_size)])
return [''.join(t) for t in a]
print(split('1234567890', 1))
print(split('1234567890', 2))
print(split('1234567890', 3))
退房
['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
['12', '34', '56', '78', '90']
['123', '456', '789']
2 years ago
请尝试以下代码:
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)))
2 years ago
这是我的解决方案:
def split_every(n, s):
return [ s[i:i+n] for i in xrange(0, len(s), n) ]
print split_every(2, "1234567890")
2 years ago
>>> 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']
2 years ago
>>> from more_itertools import sliced
>>> list(sliced('1234567890', 2))
['12', '34', '56', '78', '90']
2 years ago
A显示一组字符串的函数
def display(list):
for i in list: #To display the strings
print(i)
#Take a group of strings from keyboard
print('Enter strings separated by comma: ')
list = [x for x in input().split(",")]
#call display() and pass the list
display(list)
#output
karthik, siva, raman, inban
Enter strings separated by comma:
karthik
siva
raman
inban
2 years ago
使用lambda:
split_string = lambda x, n: [x[i:i+n] for i in range(0, len(x), n)]
s = '1234567890'
split_string(s,2)
['12', '34', '56', '78', '90']
2 years ago
>>> line = '1234567890'
>>> n = 2
>>> [line[i:i+n] for i in range(0, len(line), n)]
['12', '34', '56', '78', '90']
2 years ago
要完成,你可以用正则表达式做到这一点:
>>> import re
>>> re.findall('..','1234567890')
['12', '34', '56', '78', '90']
正如评论中指出的那样,你可以这样做:
>>> import re
>>> re.findall('..?', '123456789')
['12', '34', '56', '78', '9']
您还可以执行以下操作,以简化较长块的正则表达式:
>>> import re
>>> re.findall('.{1,2}', '123456789')
['12', '34', '56', '78', '9']
如果字符串很长,你可以使用 re.finditer 来按块生成块 .
2 years ago
我知道这个问题已经过时了,但这是最简单的方法,我知道:
def split_every_n(S, n):
return [S[i*n:(i+1)*n] for i in range(len(S) / n)]
但是,这假设您的字符串的长度是n的倍数 . 否则,你必须填写它 .