输入
input = [['Is the world population still growing?', 'Is the world population the same as the human population?', "Is the world's population still growing?", 'Are there still 2 billion people living in the world?']]
输出
output = [['Is the world population still growing?', 'Is the world population the same as the human population?', 'Is the worlds population still growing?', 'Are there still 2 billion people living in the world?']]
import re
output = [re.split('['"], ['"]', input[0][0])]
这是怎么回事?
input[0][0]
只是位于input
嵌套列表中的字符串。'['"], ['"]'
字符串文字创建以下字符串:['"], ['"]
。在这种情况下,它必须被解释为一个正则表达式,它将匹配任何字符串:- 单引号或双引号
- 后跟逗号
- 接着是一个空间
- 后跟一个单引号或双引号
注意,我们必须在单引号之前使用反斜杠
,否则解释器会认为我们想要标记字符串文字的末尾。所以反斜杠只是转义符,它们实际上并不包含在字符串中。
re.split获取
input[0][0]
字符串,并将其拆分为字符串列表,使用与上面2所述的正则表达式匹配的所有子字符串作为拆分点。最后,从3输出的列表嵌套在外部列表中,并分配给
output
(以匹配问题中提出的要求)。
这里有一个使用replace()
函数的示例解决方案:
i = """Is the world population still growing? """
print(i.replace("\", ""))
输出
Is the world population still growing?