Python论坛  - 讨论区

标题:[python-chinese] 请教一个关于引用的问题

2007年12月27日 星期四 16:54

Wayne moonbingbing在gmail.com
星期四 十二月 27 16:54:12 HKT 2007

想用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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月27日 星期四 22:46

Chuan Qin anewrer在gmail.com
星期四 十二月 27 22:46:28 HKT 2007

我以前遇到过类似的问题。
或许可以这么解决:(当时列表里的朋友介绍的)
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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月28日 星期五 08:55

Wayne moonbingbing在gmail.com
星期五 十二月 28 08:55:52 HKT 2007

这个方法不错,多谢:)

在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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年12月30日 星期日 10:58

limodou limodou在gmail.com
星期日 十二月 30 10:58:48 HKT 2007

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号