what I should change in this code to return two lists?
This question already has an answer here:
Change the last part of your program in the following way:
for tokens in token:
if tokens in words_to_match:
positive_tokens.append(tokens)
if tokens in words_to_match2:
negative_tokens.append(tokens)
return (positive_tokens, negative_tokens)
this will return a tuple with two elements. You use it as such:
(positive_tokens, negative_tokens) = ethos(yourfile)
I would do something like:
def ethos(file):
...
positive_tokens = [t for t in token if t in words_to_match]
negative_tokens = [t for t in token if t in words_to_match2]
return positive_tokens, negative_tokens
You can use this like:
positive, negative = ethos("somefile.txt")
See How do you return multiple values in Python? for a more advanced discussion on returning multiple values from functions
链接地址: http://www.djcxy.com/p/53130.html上一篇: 在Python中循环计算