我正在尝试在python中名为“ give_total_list”的函数内为全局列表分配值
total_list = list()
def give_total_list():
global total_list //should I define the global variable here?
big_list = glob.glob("drebin/feature_vectors/*")
name_list = list()
for el in big_list:
single_directory_list = el.split("/")
name = single_directory_list[2]
name_list.append(name)
total_list = list(name_list)
def main():
print (total_list)
#when I print the list, it's void.
我该如何解决?
答案 0 :(得分:0)
尝试一下:
total_list = list()
def give_total_list():
global total_list //should I define the global variable here?
big_list = glob.glob("drebin/feature_vectors/*")
name_list = list()
for el in big_list:
single_directory_list = el.split("/")
name = single_directory_list[2]
name_list.append(name)
total_list = list(name_list)
def main():
global total_list
give_total_list() #<<<<<<<<<<<<<<<
print (total_list)
main() #<<<<<<<<<<<<<<<<<
一旦调用函数,它就应该起作用。 仅定义一个函数(def ....)只会创建它,您仍然必须调用它main(),例如调用main函数