返回函数外部会出错,但打印效果很好

时间:2017-04-08 23:44:58

标签: python

以下是我的代码。我想在其他函数中使用finalList,所以我试图返回finalList,但是我收到一个错误' return'外部功能。
如果我使用print finalList,它会正确打印结果。

知道该怎么做?

import csv   
from featureVector import getStopWordList  
from preprocess import processTweet  
from featureVector import getFeatureVector  

inpTweets = csv.reader(open('sampleTweets.csv', 'rb'), delimiter=',', quotechar='|')  
stopWords = getStopWordList('stopwords.txt')   
featureList = []   
tweets = []   
for row in inpTweets: 
    sentiment = row[0]   
    tweet = row[1]  
    processedTweet = processTweet(tweet)  
    featureVector = getFeatureVector(processedTweet)  
    featureList.extend(featureVector)
    tweets.append((featureVector, sentiment));

finalList = list(set(featureList))

1 个答案:

答案 0 :(得分:-1)

  

我想在其他函数中使用finalList

你已经可以这样做了。

return finalList
  

'返回'外部功能

如果你想要def getFinalList(): # other code... return list(set(featureList)) ,那就为它做一个函数。

def foo():
    final_list = getFinalList()
    print(final_list)

foo()

选项1

def foo(final_list):
    print(final_list)

foo(getFinalList())

选项2

{{1}}