Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Split_Python 3.6 - Fatal编程技术网

将包含多个特殊字符的字符串拆分为列表,而不导入python中的任何内容

将包含多个特殊字符的字符串拆分为列表,而不导入python中的任何内容,python,split,python-3.6,Python,Split,Python 3.6,我需要做一个程序,将一个句子中的第一个单词大写,我想确保所有用于结束一个句子的特殊字符都可以使用 我不能进口任何东西!这是一个类,我只想一些例子来做这件事 我已经尝试使用if来查看列表中是否找到匹配的字符,并执行正确的拆分操作 这就是我现在拥有的功能。。。我知道它一点也不好,因为它只是返回原始字符串 def getSplit(userString): userStringList = [] if "? " in userString: userStringList

我需要做一个程序,将一个句子中的第一个单词大写,我想确保所有用于结束一个句子的特殊字符都可以使用

我不能进口任何东西!这是一个类,我只想一些例子来做这件事

我已经尝试使用if来查看列表中是否找到匹配的字符,并执行正确的拆分操作

这就是我现在拥有的功能。。。我知道它一点也不好,因为它只是返回原始字符串

def getSplit(userString):
    userStringList = []
    if "? " in userString:
        userStringList=userString.split("? ")
    elif "! " in userStringList:
        userStringList = userString.split("! ")
    elif ". " in userStringList:
        userStringList = userString.split(". ")
    else:
        userStringList = userString
    return userStringList
我希望能够输入类似
的内容,这是一个测试。这是一个测试?这绝对是一个考验

然后获取
[这是一个测试],“这是一个测试?”,“这绝对是一个测试!”]

这将把句子列表发送给另一个函数,使每个句子的第一个字母大写


这是一个旧的家庭作业,我只能使用一个特殊字符将字符串分隔成一个列表。但是我希望用户能够输入更多,而不仅仅是一种句子

我会在每个空白处拆分字符串。然后扫描列表中包含特殊字符的单词。如果有,则下一个单词大写。在最后加入列表。当然,这是假设单词之间的连续空格不超过两个

def capitalise(text):
        words = text.split()

        new_words = [words[0].capitalize()]
        i = 1
        while i < len(words) - 1:
                new_words.append(words[i])
                if "." in words[i] or "!" in words[i] or "?" in words[i]:
                        i += 1
                        new_words.append(words[i].capitalize())
                i += 1

        return " ".join(new_words)
def大写(文本):
words=text.split()
新词=[words[0]。大写()
i=1
而我
这可能是hep。使用
str.replace
将特殊字符替换为空格,并使用
str.split

Ex:

def getSplit(userString):
    return userString.replace("!", " ").replace("?", " ").replace(".", " ").split()

print(map(lambda x:x.capitalize, getSplit("sdfsdf! sdfsdfdf? sdfsfdsf.sdfsdfsd!fdfgdfg?dsfdsfgf")))

通常,您可以使用re.split(),但由于您无法导入任何内容,最好的选择是只执行for循环。这是:

def getSplit(user_input):
    n = len(user_input)
    sentences =[]
    previdx = 0
    for i in range(n - 1):
        if(user_input[i:i+2] in ['. ', '! ', '? ']):
            sentences.append(user_input[previdx:i+2].capitalize())
            previdx = i + 2
    sentences.append(user_input[previdx:n].capitalize())
    return "".join(sentences)

如果您可以使用python中默认可用的re模块,那么您可以这样做:

import re
a = 'test this. and that, and maybe something else?even without space.   or with multiple.\nor line breaks.'
print(re.sub(r'[.!?]\s*\w', lambda x: x.group(0).upper(), a))
将导致:

test this. And that, and maybe something else?Even without space.   Or with multiple.\nOr line breaks.

根据测试结果返回列表或字符串是不好的。。。请提供输入和预期输出。您可以从python标准库导入内容吗?像
re
lambda x:x.capitalize()
=>
str.capitalize