Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字符串拆分为包含多个char pro项的数组_Python_String_Function_Split - Fatal编程技术网

Python 将字符串拆分为包含多个char pro项的数组

Python 将字符串拆分为包含多个char pro项的数组,python,string,function,split,Python,String,Function,Split,我有一个字符串,我想要一个str数组,例如: "hello world" ["hel", "lo ", "wor", "ld"] 或 我看list(message)可以,但仅限于 ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", ] 有什么想法吗 >>> lst = ['he', 'llo', ' wo', 'rld'] >>> ''.join(lst) 'hello world' >&g

我有一个字符串,我想要一个str数组,例如:

"hello world"
["hel", "lo ", "wor", "ld"]

我看
list(message)
可以,但仅限于

["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", ]
有什么想法吗

>>> lst = ['he', 'llo', ' wo', 'rld']
>>> ''.join(lst)
'hello world'
>>> s = 'hello world'
>>> list(s)
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
这些是基础;如果你有任何具体的要求,请评论这篇文章,我会更新我的答案

>>> s = 'hello world'
>>> [s[i:i+3] for i in range(len(s)) if not i % 3]
['hel', 'lo ', 'wor', 'ld']
这些是基础;如果你有任何具体的要求,请评论这篇文章,我会更新我的答案

>>> s = 'hello world'
>>> [s[i:i+3] for i in range(len(s)) if not i % 3]
['hel', 'lo ', 'wor', 'ld']
要获得更通用的解决方案(即自定义拆分),请尝试以下功能:

def split_on_parts(s, *parts):
    total = 0
    buildstr = []
    for p in parts:
        buildstr.append(s[total:total+p])
        total += p
    return buildstr

s = 'hello world'
print split_on_parts(s, 3, 3, 3, 3)
print split_on_parts(s, 4, 3, 4)
它产生输出:

['hel', 'lo ', 'wor', 'ld']
['hell', 'o w', 'orld']
或者,如果你真的想买一条邮轮:

def split_on_parts(s, *parts):
    return [s[sum(parts[:p]):sum(parts[:p+1])] for p in range(len(parts))]
要获得更通用的解决方案(即自定义拆分),请尝试以下功能:

def split_on_parts(s, *parts):
    total = 0
    buildstr = []
    for p in parts:
        buildstr.append(s[total:total+p])
        total += p
    return buildstr

s = 'hello world'
print split_on_parts(s, 3, 3, 3, 3)
print split_on_parts(s, 4, 3, 4)
它产生输出:

['hel', 'lo ', 'wor', 'ld']
['hell', 'o w', 'orld']
或者,如果你真的想买一条邮轮:

def split_on_parts(s, *parts):
    return [s[sum(parts[:p]):sum(parts[:p+1])] for p in range(len(parts))]
但不确定它究竟是如何(按照什么标准)被分割的

`list` is a python key word. You can use list and indexing power of list to manipulate your result.

In [5]: s = 'hello world'

In [6]: s.split()
Out[6]: ['hello', 'world']

In [7]: list(s)
Out[7]: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

但不确定该字符串的拆分方式(根据什么标准)。

您拆分该字符串的标准是什么?i、 e.分成一定数量的部分、一定长度的部分等?您分割字符串的标准是什么?i、 e.分成一定数量的部分,分成一定长度的部分,等等?
`list` is a python key word. You can use list and indexing power of list to manipulate your result.

In [5]: s = 'hello world'

In [6]: s.split()
Out[6]: ['hello', 'world']

In [7]: list(s)
Out[7]: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']