2017-04-08 32 views
0

以下是我的代碼。我想在其他函數中使用finalList,所以我試圖返回finalList,但我得到一個錯誤「返回」外部函數。
如果我使用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)) 
+0

哪裏是你的功能? –

+0

你不能在這裏返回,你沒有定義函數 –

+0

噢好吧,但如果我想在其他函數中使用finalList輸出,我應該怎麼做 – user7511549

回答

-1

我想用finalList在其他一些功能

你已經可以做到這一點。

# other code... 
finalList = list(set(featureList)) # this is global 

def foo(): 
    print(finalList) 

foo() 

外功能

如果你想return finalList '迴歸',然後做一個功能它。

def getFinalList(): 
    # other code... 
    return list(set(featureList)) 

選項1

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

foo() 

選項2

def foo(final_list): 
    print(final_list) 

foo(getFinalList()) 
相關問題