Python:当在函数外部调用列表时,将其附加到函数中的列表不会返回任何值

Python:当在函数外部调用列表时,将其附加到函数中的列表不会返回任何值,python,list,function,text,nltk,Python,List,Function,Text,Nltk,感谢您抽出时间阅读此文章。我遇到了一个问题,调用函数外部的列表会返回空值。我想做的是查看文档,如果文档中的单词也在预定义列表中(或不在预定义列表中),则创建一个包含1(和0)的列表。接下来,我想遍历多个文档并创建一个列表。我认为下面代码中的示例将为我试图实现的目标提供更多的上下文 输入: import nltk company_list = ["This is a company that does excavation", "Last financial qua

感谢您抽出时间阅读此文章。我遇到了一个问题,调用函数外部的列表会返回空值。我想做的是查看文档,如果文档中的单词也在预定义列表中(或不在预定义列表中),则创建一个包含1(和0)的列表。接下来,我想遍历多个文档并创建一个列表。我认为下面代码中的示例将为我试图实现的目标提供更多的上下文

输入:

import nltk
company_list = ["This is a company that does excavation",
                "Last financial quarter was bad ",
                "This year we are going be exceed the projected returns."]

middle_list = []
vector = []
final_list = []
bag = ["year", "excavation", "quarter", "returns"]


def test_function():
    counter = 0
    for company in company_list:
        tokenize = nltk.word_tokenize(company)
        # eliminate the duplicates
        tokenize = set(tokenize)
        # make all the words lower case
        for word in tokenize:
            word = word.lower()
            middle_list.append(word)
        for word in bag:
            if word in middle_list:
                x = 1
            else:
                x = 0
            vector.append(x)
        # clear the middle list so that a new company's words can be put inside an empty list
        middle_list.clear()
        counter += 1
        print("Vector values: At", counter, vector)
        final_list.append(vector)
        print("List values: At", counter, final_list)
        # clear the vector so that for each company it starts with an empty list
        vector.clear()
    return final_list


test_function()
print("list outside function: ", final_list)
Vector values: At 1 [0, 1, 0, 0]
List values: At 1 [[0, 1, 0, 0]]
Vector values: At 2 [0, 0, 1, 0]
List values: At 2 [[0, 0, 1, 0], [0, 0, 1, 0]]
Vector values: At 3 [1, 0, 0, 1]
List values: At 3 [[1, 0, 0, 1], [1, 0, 0, 1], [1, 0, 0, 1]]
list outside function:  [[], [], []]
输出:

import nltk
company_list = ["This is a company that does excavation",
                "Last financial quarter was bad ",
                "This year we are going be exceed the projected returns."]

middle_list = []
vector = []
final_list = []
bag = ["year", "excavation", "quarter", "returns"]


def test_function():
    counter = 0
    for company in company_list:
        tokenize = nltk.word_tokenize(company)
        # eliminate the duplicates
        tokenize = set(tokenize)
        # make all the words lower case
        for word in tokenize:
            word = word.lower()
            middle_list.append(word)
        for word in bag:
            if word in middle_list:
                x = 1
            else:
                x = 0
            vector.append(x)
        # clear the middle list so that a new company's words can be put inside an empty list
        middle_list.clear()
        counter += 1
        print("Vector values: At", counter, vector)
        final_list.append(vector)
        print("List values: At", counter, final_list)
        # clear the vector so that for each company it starts with an empty list
        vector.clear()
    return final_list


test_function()
print("list outside function: ", final_list)
Vector values: At 1 [0, 1, 0, 0]
List values: At 1 [[0, 1, 0, 0]]
Vector values: At 2 [0, 0, 1, 0]
List values: At 2 [[0, 0, 1, 0], [0, 0, 1, 0]]
Vector values: At 3 [1, 0, 0, 1]
List values: At 3 [[1, 0, 0, 1], [1, 0, 0, 1], [1, 0, 0, 1]]
list outside function:  [[], [], []]
预期结果:[0,1,0,0],[0,0,1,0],[1,0,0,1]

如您所见,存在两个问题:

1) 当我在函数中打印列表时,它会返回一个向量列表,但向量是重复的(我不想要) 2) 当我想在函数外部打印列表时,它会返回一个包含3个列表的列表,但每个列表都是空的


谢谢你的时间和帮助

我查看了您的代码,如果您在vector.clear()之后添加一个打印,我想您会看到发生了什么

Final_列表只包含对vector的引用,因此当您清除该引用时,它也将清除Final_列表中的内容

改变

final_list.append(vector)


您必须捕获函数的返回,比如final_list=test_function()@ChristianSloper我已经尝试过了,但是打印的列表(函数外部)仍然会返回一个空列表列表…现在可以工作了。感谢您抽出时间对其进行更多的研究!