Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/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 - Fatal编程技术网

Python 试图从文本中删除句子列表,只删除第一个字符

Python 试图从文本中删除句子列表,只删除第一个字符,python,Python,我上了下面的课 class SentenceReducer(): def getRidOfSentences(self, line, listSentences): for i in listSentences: print(i) return line.replace(i, '') strings = 'This is a' def stripSentences(self, aTranscript):

我上了下面的课

class SentenceReducer():
    def getRidOfSentences(self, line, listSentences):
        for i in listSentences:
            print(i)
            return line.replace(i, '')

    strings = 'This is a'
    def stripSentences(self, aTranscript):
        result = [self.getRidOfSentences(line, self.strings) for line in aTranScript]
        return(result)
它基本上应该吃一个数据帧,然后逐行检查相关行是否包含本例中列表句子1中的一个句子

但是,当我创建一个新类时

newClass = SentenceReducer()
并使用以下数据运行脚本

aTranScript = [ 'This is a test', 'This is not a test']
new_df = newClass.stripSentences(aTranScript)
它会删除原始数据中的“T”。但它应该取代整个句子“这是一个”。如果我加上printi,它也会打印T

你有什么想法吗

首先,aTranscript和aTranscript不是同一个变量,注意后者中的资本s

第二,您应该使用self.listQuestions或SentenceReducer.listQuestions访问ListQuestions

第三,您使用的字符串没有在任何地方声明

最后,函数stripQuences不返回任何内容。

在getRidOfSentences中,变量listQuences的值为“This is a”,这是一个字符串

对字符串进行迭代会生成单个字符:

>>> strings = 'This is a'
>>> for x in strings:
...     print(x)
T
h
i
s

i
s

a
>>> strings = ['This is a']
>>> for x in strings:
...     print(x)
This is a
您希望将此字符串放入列表中,以便在该列表上迭代得到整个字符串,而不是单个字符:

>>> strings = 'This is a'
>>> for x in strings:
...     print(x)
T
h
i
s

i
s

a
>>> strings = ['This is a']
>>> for x in strings:
...     print(x)
This is a

另一个问题:for循环中的返回意味着函数在第一次迭代结束时退出,这就是为什么您只看到T,而没有看到h、i、s等等。

这是您的实际缩进吗?请修复它,并尝试使用标准的4-空格,以帮助可读性。我至少看到了几个潜在的逻辑错误。请准确地向我们展示您正在运行的内容。最好使其成为init函数中的一个成员。在任何情况下,您已经将ListSequences定义为类级别变量,因此您需要访问它。老实说,我认为没有必要,你应该把它作为你的方法已经做过的一个参数来传递……标题中提到的命名错误发生在哪里?谢谢你的反馈。我根据您的建议更改了代码,请参见编辑。你知道为什么我的第一个函数中的i被打印为TThanks mkrieger1吗,真的很有用!