我是一个新手,我写了一个tokenize函数,它基本上接受一个由句子组成的txt文件,并根据空格和标点拆分它们。这里的东西是它给了我一个父列表中的子列表的输出。分割python列表
我的代码:
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']
如何删除父列表中的出我的输出?我需要在代码中进行哪些更改才能删除外部列表括号?
为什么要取出外支架?你有一个列表子列表。 – 2015-01-21 06:51:39
我不希望子列表实际出现在我的输出中。 – Wolf 2015-01-21 06:53:06
然后你会得到一个元组(用'()')。你是否试图以特定的方式打印出来? – 2015-01-21 06:53:35