Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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将列表逐点拆分为一个句子_Python_Regex_Python 3.x - Fatal编程技术网

使用python将列表逐点拆分为一个句子

使用python将列表逐点拆分为一个句子,python,regex,python-3.x,Python,Regex,Python 3.x,我有一个句子列表: ['hello', 'I would like to thank you', 'I would like to thank you. By the way'] 当我找到“”时,我需要将每个句子分成一个列表 例如,在上面的示例中,预期结果是: ['hello', 'I would like to thank you', 'I would like to thank you'. 'By the way'] 我尝试用python编写以下代码: def split_pint(re

我有一个句子列表:

['hello', 'I would like to thank you', 'I would like to thank you. By the way']
当我找到“”时,我需要将每个句子分成一个列表

例如,在上面的示例中,预期结果是:

['hello', 'I would like to thank you', 'I would like to thank you'. 'By the way']
我尝试用python编写以下代码:

def split_pint(result):
    for i in result:
        i = re.split(r". ", i)
    return result
但判决没有分开

有什么想法吗


感谢您使用简单的迭代和str.split

Ex:

data = ['hello', 'I would like to thank you', 'I would like to thank you. By the way']

def split_pint(data):
    result = []
    for elem in data:
        result.extend(elem.split(". "))        
    return result

print(split_pint(data))
['hello', 'I would like to thank you', 'I would like to thank you', 'By the way']
输出:

data = ['hello', 'I would like to thank you', 'I would like to thank you. By the way']

def split_pint(data):
    result = []
    for elem in data:
        result.extend(elem.split(". "))        
    return result

print(split_pint(data))
['hello', 'I would like to thank you', 'I would like to thank you', 'By the way']

如您所见,这不是修改列表的方法:

l = [0, 0]
for x in l:
    x = 1
print(l)
# [0, 0]
无论如何,如果要使用
re.split
,则需要转义
字符:

import re

l = ['hello', 'I would like to thank you', 'I would like to thank you. By the way']
def split_pint(result):
    res = []
    for i in result:
        res += re.split("\. ", i)
    return res


print(split_pint(l))
['hello', 'I would like to thank you', 'I would like to thank you', 'By the way']



另一个选项,但只有一个线性和函数式编程方式:

>>> from functools import reduce
>>> a = ['hello', 'I would like to thank you', 'I would like to thank you. By the way']
>>> reduce(lambda i, j: i + j, map(lambda s: s.split('. '), a))
['hello', 'I would like to thank you', 'I would like to thank you', 'By the way']
首先,
map
从每个字符串生成一个列表,其次,
reduce
只是将所有列表串联起来