Python 按长度将字符串拆分为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13673060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split string into strings by length?
提问by tkbx
Is there a way to take a string that is 4*xcharacters long, and cut it into 4 strings, each xcharacters long, without knowing the length of the string?
有没有办法在不知道字符串长度的情况下将4*x一个x字符长的字符串切成4个字符串,每个字符长?
For example:
例如:
>>>x = "qwertyui"
>>>split(x, one, two, three, four)
>>>two
'er'
采纳答案by Alexander
>>> x = "qwertyui"
>>> chunks, chunk_size = len(x), len(x)/4
>>> [ x[i:i+chunk_size] for i in range(0, chunks, chunk_size) ]
['qw', 'er', 'ty', 'ui']
回答by fnkr
I tried Alexanders answer but got this error in Python3:
我尝试了 Alexanders 的回答,但在 Python3 中遇到了这个错误:
TypeError: 'float' object cannot be interpreted as an integer
类型错误:“float”对象不能解释为整数
This is because the division operator in Python3 is returning a float. This works for me:
这是因为 Python3 中的除法运算符返回一个浮点数。这对我有用:
>>> x = "qwertyui"
>>> chunks, chunk_size = len(x), len(x)//4
>>> [ x[i:i+chunk_size] for i in range(0, chunks, chunk_size) ]
['qw', 'er', 'ty', 'ui']
Notice the //at the end of line 2, to ensure truncation to an integer.
注意第//2 行末尾的 ,以确保截断为整数。
回答by bobchase
Here is a one-liner that doesn't need to know the length of the string beforehand:
这是一个不需要事先知道字符串长度的单行:
from functools import partial
from StringIO import StringIO
[l for l in iter(partial(StringIO(data).read, 4), '')]
If you have a file or socket, then you don't need the StringIO wrapper:
如果您有文件或套接字,则不需要 StringIO 包装器:
[l for l in iter(partial(file_like_object.read, 4), '')]
回答by Nils
And for dudes who prefer it to be a bit more readable:
对于喜欢它更具可读性的人:
def itersplit_into_x_chunks(string,x=10): # we assume here that x is an int and > 0
size = len(string)
chunksize = size//x
for pos in range(0, size, chunksize):
yield string[pos:pos+chunksize]
output:
输出:
>>> list(itersplit_into_x_chunks('qwertyui',x=4))
['qw', 'er', 'ty', 'ui']
回答by Moisey Oysgelt
My solution
我的解决方案
st =' abs de fdgh 1234 556 shg shshh'
print st
def splitStringMax( si, limit):
ls = si.split()
lo=[]
st=''
ln=len(ls)
if ln==1:
return [si]
i=0
for l in ls:
st+=l
i+=1
if i <ln:
lk=len(ls[i])
if (len(st))+1+lk < limit:
st+=' '
continue
lo.append(st);st=''
return lo
############################
print splitStringMax(st,7)
# ['abs de', 'fdgh', '1234', '556', 'shg', 'shshh']
print splitStringMax(st,12)
# ['abs de fdgh', '1234 556', 'shg shshh']
回答by Krister Hedfors
def split2len(s, n):
def _f(s, n):
while s:
yield s[:n]
s = s[n:]
return list(_f(s, n))
回答by thodnev
Here are two generic approaches. Probably worth adding to your own lib of reusables. First one requires the item to be sliceable and second one works with any iterables (but requires their constructor to accept iterable).
这里有两种通用方法。可能值得添加到您自己的可重用库中。第一个要求项目是可切片的,第二个适用于任何可迭代对象(但要求它们的构造函数接受可迭代对象)。
def split_bylen(item, maxlen):
'''
Requires item to be sliceable (with __getitem__ defined)
'''
return [item[ind:ind+maxlen] for ind in range(0, len(item), maxlen)]
#You could also replace outer [ ] brackets with ( ) to use as generator.
def split_bylen_any(item, maxlen, constructor=None):
'''
Works with any iterables.
Requires item's constructor to accept iterable or alternatively
constructor argument could be provided (otherwise use item's class)
'''
if constructor is None: constructor = item.__class__
return [constructor(part) for part in zip(* ([iter(item)] * maxlen))]
#OR: return map(constructor, zip(* ([iter(item)] * maxlen)))
# which would be faster if you need an iterable, not list
So, in topicstarter's case, the usage is:
所以,在 topicstarter 的情况下,用法是:
string = 'Baboons love bananas'
parts = 5
splitlen = -(-len(string) // parts) # is alternative to math.ceil(len/parts)
first_method = split_bylen(string, splitlen)
#Result :['Babo', 'ons ', 'love', ' ban', 'anas']
second_method = split_bylen_any(string, splitlen, constructor=''.join)
#Result :['Babo', 'ons ', 'love', ' ban', 'anas']
回答by pmsh.93
The string splitting is required in many cases like where you have to sort the characters of the string given, replacing a character with an another character etc. But all these operations can be performed with the following mentioned string splitting methods.
在许多情况下需要进行字符串拆分,例如您必须对给定字符串的字符进行排序,用另一个字符替换一个字符等。但是所有这些操作都可以使用以下提到的字符串拆分方法来执行。
The string splitting can be done in two ways:
字符串拆分可以通过两种方式完成:
Slicing the given string based on the length of split.
Converting the given string to a list with list(str) function, where characters of the string breakdown to form the the elements of a list. Then do the required operation and join them with 'specified character between the characters of the original string'.join(list) to get a new processed string.
根据拆分的长度对给定的字符串进行切片。
使用 list(str) 函数将给定的字符串转换为列表,其中字符串的字符分解为列表的元素。然后进行所需的操作,并用'原始字符串的字符之间的指定字符'.join(list) 将它们连接起来,得到一个新的处理过的字符串。
回答by Zhang Tong
l = 'abcdefghijklmn'
def group(l,n):
tmp = len(l)%n
zipped = zip(*[iter(l)]*n)
return zipped if tmp == 0 else zipped+[tuple(l[-tmp:])]
print group(l,3)
回答by Eric
Got an retrick:
有re窍门:
In [28]: import re
In [29]: x = "qwertyui"
In [30]: [x for x in re.split(r'(\w{2})', x) if x]
Out[30]: ['qw', 'er', 'ty', 'ui']
Then be a func, it might looks like:
然后是一个func,它可能看起来像:
def split(string, split_len):
# Regex: `r'.{1}'` for example works for all characters
regex = r'(.{%s})' % split_len
return [x for x in re.split(regex, string) if x]

