IPython如何处理局部变量?我有这个函数,它可以在Python shell中工作,但不能在IPython shell中工作。
def change(key,value):
global aname
global alist
alist.append(key)
aname.extend(value)
我在一个for
循环中使用它,它从JSON和其他.txt文件中读取输入,并将键和值添加到一个列表中,然后由另一个函数使用该列表保存到数据库中。如果我不这样做,它将是丑陋的,并将使用我的循环中的索引。
[change(key,value) for key,value in jsondata.itervalues()]
def storeindatabase():
do_sothing to the list aname and store
do_sothing to the alist and store
发布于 2019-04-22 01:05:30
除非列表在第一次被调用时已经存在,否则Append和extend将不起作用。预先声明它们会让它在notebook中也能正常工作。
aname=[]
alist=[]
def change(key,value):
global aname
global alist
alist.append(key)
aname.extend(value)
change(3,[3])
print(alist)
[3]
print(aname)
[4]
https://stackoverflow.com/questions/16924918
复制相似问题