2017-04-08 7 views
0

以下は私のコードです。 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)) 
+0

あなたの関数である

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

オプション1

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

オプション? –

+0

ここに戻ることはできません。関数が定義されていません。 –

+0

ああ大丈夫ですが、他の関数でfinalList出力を使用したい場合はどうすればいいですか? – user7511549

答えて

-1

私はいくつかの他の機能に

をfinalListを使用したいあなたはすでにこれを行うことができます。

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

def foo(): 
    print(finalList) 

foo() 

'リターン' 外の機能

あなたがreturn finalListにしたい場合は、それを機能させます。 2

def foo(final_list): 
    print(final_list) 

foo(getFinalList()) 
関連する問題