2007年12月27日 星期四 16:54
想用python的字典和列表来模拟数据库的表结构,比如下面的代码; >>> a = {'name':'','ad':'','age':''} #表结构 >>> b = [a,a,a,a] >>> b[0]['ad'] = 1 #本意是只改变第一个a的ad值,但因为a是引用,所以改变了所有a >>> b [{'age': '', 'name': '', 'ad': 1}, {'age': '', 'name': '', 'ad': 1}, {'age': '','name': '', 'ad': 1}, {'age': '', 'name': '', 'ad': 1}] 不知道有没有方便的方法解决这个问题.如果用b = [a.copy(),a.cpoy(),a.copy()],感觉太麻烦了 多谢 -- wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20071227/def43b58/attachment.html
2007年12月27日 星期四 22:46
我以前遇到过类似的问题。 或许可以这么解决:(当时列表里的朋友介绍的) b = [{'name':'','ad':'','age':''} for i in range(3)] 在07-12-27,Wayne <moonbingbing在gmail.com> 写道: > > 想用python的字典和列表来模拟数据库的表结构,比如下面的代码; > >>> a = {'name':'','ad':'','age':''} #表结构 > >>> b = [a,a,a,a] > >>> b[0]['ad'] = 1 #本意是只改变第一个a的ad值,但因为a是引用,所以改变了所有a > >>> b > [{'age': '', 'name': '', 'ad': 1}, {'age': '', 'name': '', 'ad': 1}, > {'age': '','name': '', 'ad': 1}, {'age': '', 'name': '', 'ad': 1}] > > 不知道有没有方便的方法解决这个问题.如果用b = [a.copy(),a.cpoy(),a.copy()],感觉太麻烦了 > 多谢 > -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20071227/dc2a602a/attachment.html
2007年12月28日 星期五 08:55
这个方法不错,多谢:) 在07-12-27,Chuan Qin <anewrer at gmail.com> 写道: > > 我以前遇到过类似的问题。 > 或许可以这么解决:(当时列表里的朋友介绍的) > b = [{'name':'','ad':'','age':''} for i in range(3)] > > 在07-12-27,Wayne <moonbingbing at gmail.com> 写道: > > > > 想用python的字典和列表来模拟数据库的表结构,比如下面的代码; > > >>> a = {'name':'','ad':'','age':''} #表结构 > > >>> b = [a,a,a,a] > > >>> b[0]['ad'] = 1 #本意是只改变第一个a的ad值,但因为a是引用,所以改变了所有a > > >>> b > > [{'age': '', 'name': '', 'ad': 1}, {'age': '', 'name': '', 'ad': 1}, > > {'age': '','name': '', 'ad': 1}, {'age': '', 'name': '', 'ad': 1}] > > > > 不知道有没有方便的方法解决这个问题.如果用b = [a.copy(),a.cpoy(),a.copy()],感觉太麻烦了 > > 多谢 > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -- wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20071228/78a45e90/attachment.html
2007年12月30日 星期日 10:58
On Dec 27, 2007 4:54 PM, Wayne <moonbingbing在gmail.com> wrote: > 想用python的字典和列表来模拟数据库的表结构,比如下面的代码; > >>> a = {'name':'','ad':'','age':''} #表结构 > >>> b = [a,a,a,a] > >>> b[0]['ad'] = 1 #本意是只改变第一个a的ad值,但因为a是引用,所以改变了所有a > >>> b > [{'age': '', 'name': '', 'ad': 1}, {'age': '', 'name': '', 'ad': 1}, {'age': > '','name': '', 'ad': 1}, {'age': '', 'name': '', 'ad': 1}] > > 不知道有没有方便的方法解决这个问题.如果用b = [a.copy(),a.cpoy(),a.copy()],感觉太麻烦了 > 多谢 可以使用copy模块的deepcopy函数。 -- I like python! UliPad <>: http://code.google.com/p/ulipad/ meide < >: http://code.google.com/p/meide/ My Blog: http://www.donews.net/limodou
Zeuux © 2025
京ICP备05028076号