2017-02-18 69 views
-1

所以導入陣列,我有一個main.pyfile.pyfile.py我有一個功能(例如:Python從一個不同的文件

def s_break(message): 
    words = message.split(" ") 

,和陣列words

當我將數組數組導入main.py使用:from "filename" import words我收到數組爲空。爲什麼?

謝謝!

+1

你需要表現出更多的代碼,我沒有想法你在做什麼,什麼可能是錯誤 –

+0

所以,我在file.py中有一個函數,它將插入到數組中的字符串分開。現在,當我想在另一個文件中使用該數組時,它就變爲空。 –

回答

0

你需要實際調用s_break函數,否則你只會得到空的列表/數組。

test_file.py:

message = 'a sample string this represents' 
list_of_words = [] 

def s_break(message): 
    words = message.split(" ") 
    for w in words: 
     list_of_words.append(w) 

s_break(message) # call the function to populate the list 

然後在main.py

from test_file import list_of_words 

print list_of_words 

輸出:

>>> ['a', 'sample', 'string', 'this', 'represents'] 
+0

謝謝! 原來,我不得不在s_break()的參數list_of_words數組添加。 –

相關問題