[英]Splitting python lists
我是新手,我写了一个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中的函数只能返回一个值。 如果要返回两件事(例如,您有两个单词列表),则必须返回一个可以容纳两个东西的对象,例如列表,元组和字典。
不要混淆您要如何打印输出与返回的对象是什么。
要简单地打印列表:
for b in words:
print(re.findall(r'\w+', b))
如果执行此操作,则您的方法将不返回任何内容(实际上返回None
)。
要返回两个列表:
return [re.findall(r'\w+', b) for b in words]
然后像这样调用您的方法:
word_lists = tokenize(document)
for word_list in word_lists:
print(word_list)
这应该工作
print ','.join([re.findall(r'\w+', b) for b in words])
我有一个例子,我想这个例子与您遇到的问题没有太大不同...
在这里我只占列表的一部分。
>>> a = [['sa', 'bbb', 'ccc'], ['dad', 'des', 'kkk']]
>>>
>>> print a[0], a[1]
['sa', 'bbb', 'ccc'] ['dad', 'des', 'kkk']
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.