what I should change in this code to return two lists?

This question already has an answer here:

  • How do you return multiple values in Python? 13 answers

  • 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中循环计算

    下一篇: 在这段代码中我应该改变什么来返回两个列表?