我是个新手,我写了一个tokenize函数,它基本上接收一个由句子组成的txt文件,并根据空格和标点符号将它们拆分。这里的问题是,它给了我一个在父列表中存在子列表的输出。你知道吗
我的代码:
def tokenize(document)
file = open("document.txt")
text = file.read()
hey = text.lower()
words = re.split(r'\s\s+', hey)
print [re.findall(r'\w+', b) for b in words]
我的输出:
[['what', 's', 'did', 'the', 'little', 'boy', 'tell', 'the', 'game', 'eggs', 'warden'], ['his', 'dad', 'was', 'warden', 'in', 'the', 'kitchen', 'poaching', 'eggs']]
期望输出:
['what', 's', 'did', 'the', 'little', 'boy', 'tell', 'the', 'game', 'eggs', 'warden']['his', 'dad', 'was', 'warden', 'in', 'the', 'kitchen', 'poaching', 'eggs']
如何删除输出中的父列表??我需要在代码中做什么更改才能删除外部列表括号??你知道吗
我有一个例子,我想这和你的问题没什么不同。。。你知道吗
我只拿了名单的一部分。你知道吗
这应该管用
Python中的函数只能返回一个值。如果你想返回两个东西(例如,在你的例子中,有两个单词列表),你必须返回一个可以包含两个东西的对象,比如一个列表,一个元组,一个字典。你知道吗
不要混淆打印输出的方式与返回的对象是什么。你知道吗
只需打印列表:
如果您这样做,那么您的方法不会返回任何内容(它实际上返回
None
)。你知道吗要返回两个列表:
然后像这样调用您的方法:
相关问题 更多 >
编程相关推荐