2017-02-18 68 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数组添加。 –

相关问题