Python论坛  - 讨论区

标题:[python-chinese] 问关于三维数组在python中的表达

2005年05月01日 星期日 11:05

jawbreaker jawbreaker at 163.com
Sun May 1 11:05:56 HKT 2005

在C中,我们会这样定义和初始化这样一个三维数组:
 int Table[12][12][12];
 for(i=0;i<12;i++)
        for(j=0;j<12;j++)
            for(k=0;k<12;k++)
                Table[i][j][k]=1;
那我在Python中用如下方式是否最简单:
        Table = []
        i = 0
        while i < 12 :
            j = 0
            Table.append([])
            while j < 12 :
                k = 0
                Table[i].append([])
                while k < 12 :
                    Table[i][j].append(1)
                    k+=1
                j+=1
            i+=1
我想这个方法还是比较繁杂,不知道各位有没有更简单一点的方法?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050501/cc9bcade/attachment.html

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

2005年05月01日 星期日 11:50

Qiangning Hong hongqn at gmail.com
Sun May 1 11:50:17 HKT 2005

On 5/1/05, jawbreaker <jawbreaker at 163.com> wrote:
> 
> 在C中,我们会这样定义和初始化这样一个三维数组:
> int Table[12][12][12];
> for(i=0;i<12;i++)
> for(j=0;j<12;j++)
> for(k=0;k<12;k++)
> Table[i][j][k]=1;
> 

Table = [[[[1] * 12] for x in range(12)] for x in range(12)]

-- 
Qiangning Hong
Get Firefox! <
http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050501/42506123/attachment.htm

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

2005年05月01日 星期日 11:51

Qiangning Hong hongqn at gmail.com
Sun May 1 11:51:57 HKT 2005

On 5/1/05, Qiangning Hong <hongqn at gmail.com> wrote:
> 
> On 5/1/05, jawbreaker <jawbreaker at 163.com> wrote:
> > 
> > 在C中,我们会这样定义和初始化这样一个三维数组:
> > int Table[12][12][12];
> > for(i=0;i<12;i++)
> > for(j=0;j<12;j++)
> > for(k=0;k<12;k++)
> > Table[i][j][k]=1;
> > 
> 
> Table = [[[[1] * 12] for x in range(12)] for x in range(12)]
> 

不好意思,写错了,应该是:
Table = [ [ [1] * 12 for x in range(12) ] for x in range(12) ]

-- 
Qiangning Hong
Get Firefox! <
http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050501/efd299d1/attachment.html

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

2005年05月01日 星期日 12:11

William Wu william at sinowish.com
Sun May 1 12:11:16 HKT 2005

[[[1]*12]*12]*12

Qiangning Hong wrote:

> On 5/1/05, *Qiangning Hong* <hongqn at gmail.com
> hongqn at gmail.com>> wrote:
>
>     On 5/1/05, *jawbreaker* <jawbreaker at 163.com
>     jawbreaker at 163.com>> wrote:
>
>         在C中,我们会这样定义和初始化这样一个三维数组:
>         int Table[12][12][12];
>         for(i=0;i<12;i++)
>         for(j=0;j<12;j++)
>         for(k=0;k<12;k++)
>         Table[i][j][k]=1;
>
>
>     Table = [[[[1] * 12] for x in range(12)] for x in range(12)]
>
>
> 不好意思,写错了,应该是:
> Table = [ [ [1] * 12 for x in range(12) ] for x in range(12) ]
>
> -- 
> Qiangning Hong
> Get Firefox!
> <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1
> <http://www.spreadfirefox.com/?q=affiliates&id;=67907&t;=1>>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>  
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: william.vcf
Type: text/x-vcard
Size: 154 bytes
Desc: not available
Url : http://lists.exoweb.net/pipermail/python-chinese/attachments/20050501/5a326f6d/william.vcf

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

2005年05月01日 星期日 13:06

Qiangning Hong hongqn at gmail.com
Sun May 1 13:06:28 HKT 2005

这个构造出来的不是三维数组,修改其中一个元素就会导致其他元素发生变化。

例如:

.>>> x = [[[1]*3]*3]*3
.>>> x
[[[1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 
1, 1], [1, 1, 1], [1, 1, 1]]]
.>>> x[1][1][1] = 2
.>>> x
[[[1, 2, 1], [1, 2, 1], [1, 2, 1]], [[1, 2, 1], [1, 2, 1], [1, 2, 1]], [[1, 
2, 1], [1, 2, 1], [1, 2, 1]]]



On 5/1/05, William Wu <william at sinowish.com> wrote:
> 
> [[[1]*12]*12]*12
> 
> Qiangning Hong wrote:
> 
> > On 5/1/05, *Qiangning Hong* <hongqn at gmail.com
> > hongqn at gmail.com>> wrote:
> >
> > On 5/1/05, *jawbreaker* <jawbreaker at 163.com
> > jawbreaker at 163.com>> wrote:
> >
> > 在C中,我们会这样定义和初始化这样一个三维数组:
> > int Table[12][12][12];
> > for(i=0;i<12;i++)
> > for(j=0;j<12;j++)
> > for(k=0;k<12;k++)
> > Table[i][j][k]=1;
> >
> >
> > Table = [[[[1] * 12] for x in range(12)] for x in range(12)]
> >
> >
> > 不好意思,写错了,应该是:
> > Table = [ [ [1] * 12 for x in range(12) ] for x in range(12) ]
> >
> > --
> > Qiangning Hong
> > Get Firefox!
> > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1
> > <http://www.spreadfirefox.com/?q=affiliates&id;=67907&t;=1>>
> >
> >------------------------------------------------------------------------
> >
> >_______________________________________________
> >python-chinese list
> >python-chinese at lists.python.cn
> >http://python.cn/mailman/listinfo/python-chinese
> >
> >
> 
> 
> 


-- 
Qiangning Hong
Get Firefox! <
http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050501/5337af5a/attachment.htm

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号