python 静态问题

lin422 2010-09-15 11:06:57
在网上查了些资料 还是不是很清楚

程序运行一次得到一个List,

再次执行程序得到的新的List, 和旧的list比较

看看list是否有改变

不知道如何保存第一次的list, 供下次使用。

谢谢。
...全文
106 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lin422 2010-09-21
  • 打赏
  • 举报
回复
谢谢大家!
guoxuqu 2010-09-16
  • 打赏
  • 举报
回复
上面这段代码可以参考一下。
guoxuqu 2010-09-16
  • 打赏
  • 举报
回复
#!/usr/bin/python
# Filename: pickling.py

import cPickle as p
#import pickle as p

shoplistfile = 'shoplist.data'
# the name of the file where we will store the object

shoplist = ['apple', 'mango', 'carrot']

# Write to the file
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()

del shoplist # remove the shoplist

# Read back from the storage
f = file(shoplistfile)
storedlist = p.load(f)
print storedlist


guoxuqu 2010-09-16
  • 打赏
  • 举报
回复
用cPickle模块可以保存对象。使用它你可以在一个文件中储存任何Python对象,之后你又可以把它完整无缺地取出来。这被称为 持久地 储存对象。
thy38 2010-09-16
  • 打赏
  • 举报
回复
用json也行
iambic 2010-09-16
  • 打赏
  • 举报
回复
保存到硬盘上,方法很多。Python里比较省事的是pickle了。你也可以用cPickle,但是我一般只有在真的需要性能的时候才用,原因很简单,cPickle的名字看起来丑。就像我很少用xrange代替range。

37,738

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧