在python中尝试为全局变量赋值时出现问题
我正在尝试为python中名为“give_total_list”的函数中的全局列表赋值在python中尝试为全局变量赋值时出现问题,python,list,global,Python,List,Global,我正在尝试为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_li
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.
我如何解决这个问题?试试这个:
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() #<<<<<<<<<<<<<<<<<
total_list=list()
def给出_总计_列表():
全局总清单//我应该在这里定义全局变量吗?
大列表=glob.glob(“drebin/feature\u vectors/*”)
name_list=list()
对于大列表中的el:
单目录列表=el.split(“/”)
名称=单个目录列表[2]
名称\u列表。附加(名称)
总列表=列表(名称列表)
def main():
全球总量清单
give_total_list()#如何/在何处调用这两个函数?抱歉,我使用的是PythonYes,但代码中应该有一些其他行,比如如果uuu name_uu=='u main_uu':
然后调用这两个函数。。。。