Python论坛  - 讨论区

标题:Re: [python-chinese] 请问,怎么声明一个二维的数组啊?

2005年09月01日 星期四 10:28

Levin Du levin.du at gmail.com
Thu Sep 1 10:28:51 HKT 2005

简化的写法是错误的写法

在 05-8-31,倪正刚<ni at twinisa.com> 写道:
> 嗯,我想问一个问题。
> 
> 不论是简化的写法,还是原来比较复杂的写法。在性能上是否存在差异?
> 
> 
> 
> 
> -----Original Message-----
> From: python-chinese-bounces at lists.python.cn
> [mailto:python-chinese-bounces at lists.python.cn]On Behalf Of limodou
> Sent: 2005年8月31日 15:31
> To: python-chinese at lists.python.cn
> Subject: Re: [python-chinese] 请问,怎么声明一个二维的数组啊?
> 
> 
> 在 05-8-31,Jerry<jetport at gmail.com> 写道:
> >
> > > Leo Jay wrote:
> > > > 我想生成一个二维的整数数组,并且数组里的每个元素赋初值-1
> > > > 我现在的写法如下:
> > > > board = [ [-1 for i in range(10)]  for j in range(10) ]
> > > >
> > > > 有没有什么简短一点的写法啊?二维数组都这么长,那如果是4维的不是要疯掉
> 了?
> >
> >
> >
> > 更短的写法:)
> >
> > board = [ [-1] * 10 ] * 10
> >
> 
> 这就是我想说的问题,你改了一个,其它的也都改了。简化一下:
> 
>  >>> board = [ [-1] * 2 ] * 2
>  >>> board
>  [[-1, -1], [-1, -1]]
>  >>> board[0][0] = 1
>  >>> board
>  [[1, -1], [1, -1]]
> 
> --
> I like python!
> My Donews Blog: http://www.donews.net/limodou
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>

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

2005年09月01日 星期四 20:50

倪正刚 ni at twinisa.com
Thu Sep 1 20:50:27 HKT 2005

谢谢。

我的意思不是在讨论这个问题本身。我是说,对于比较普遍的情况,简化的写法相对于
最基本的写法,是不是性能上稍逊?
我在PostgreSQL上有这样的经验,简单的写法,有时候性能更低。


-----Original Message-----
From: Levin Du [mailto:levin.du at gmail.com]
Sent: 2005年9月1日 10:29
To: ni at twinisa.com; python-chinese at lists.python.cn
Subject: Re: [python-chinese] 请问,怎么声明一个二维的数组啊?


简化的写法是错误的写法

在 05-8-31,倪正刚<ni at twinisa.com> 写道:
> 嗯,我想问一个问题。
>
> 不论是简化的写法,还是原来比较复杂的写法。在性能上是否存在差异?
>
>
>
>
> -----Original Message-----
> From: python-chinese-bounces at lists.python.cn
> [mailto:python-chinese-bounces at lists.python.cn]On Behalf Of limodou
> Sent: 2005年8月31日 15:31
> To: python-chinese at lists.python.cn
> Subject: Re: [python-chinese] 请问,怎么声明一个二维的数组啊?
>
>
> 在 05-8-31,Jerry<jetport at gmail.com> 写道:
> >
> > > Leo Jay wrote:
> > > > 我想生成一个二维的整数数组,并且数组里的每个元素赋初值-1
> > > > 我现在的写法如下:
> > > > board = [ [-1 for i in range(10)]  for j in range(10) ]
> > > >
> > > > 有没有什么简短一点的写法啊?二维数组都这么长,那如果是4维的不是要疯
掉
> 了?
> >
> >
> >
> > 更短的写法:)
> >
> > board = [ [-1] * 10 ] * 10
> >
>
> 这就是我想说的问题,你改了一个,其它的也都改了。简化一下:
>
>  >>> board = [ [-1] * 2 ] * 2
>  >>> board
>  [[-1, -1], [-1, -1]]
>  >>> board[0][0] = 1
>  >>> board
>  [[1, -1], [1, -1]]
>
> --
> I like python!
> My Donews Blog: http://www.donews.net/limodou
>
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>


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

2005年09月02日 星期五 09:41

nEO (a.k.a. gentoo.cn) gentoo.cn at gmail.com
Fri Sep 2 09:41:22 HKT 2005

在 05-9-1,倪正刚<ni at twinisa.com> 写道:
> 我的意思不是在讨论这个问题本身。我是说,对于比较普遍的情况,简化的写法相对于
> 最基本的写法,是不是性能上稍逊?
> 我在PostgreSQL上有这样的经验,简单的写法,有时候性能更低。
具体的语句需要实验才知道,有可能正好相反
我记得i += 1比 i = i + 1就要快

-- 
I'm the one, powered by nEO

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

2005年09月02日 星期五 16:39

Person person.lee at gmail.com
Fri Sep 2 16:39:10 HKT 2005

如果生成二维数组,我曾经用过两种方法。
以前用fa函数,后来用fb函数,要比fa快几倍。

def fa(i,j):
    array = [[-1 for x in range(i)][:] for y in range(j)]
    return array

def fb(i,j):
    ai = [-1 for x in range(i)]
    array = [ai[:] for x in range(j)]
    return array

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

2005年09月02日 星期五 17:51

Qiangning Hong hongqn at gmail.com
Fri Sep 2 17:51:39 HKT 2005

Person wrote:
> 如果生成二维数组,我曾经用过两种方法。
> 以前用fa函数,后来用fb函数,要比fa快几倍。
> 
> def fa(i,j):
>     array = [[-1 for x in range(i)][:] for y in range(j)]
>     return array

这个[:]是没有必要的,因为针对x的list comprehension会在每次y迭代时重新执行。
如果i, j比较大的话,用xrange替代range会有较大的性能提高。
如果数组内元素类型均为数值的话,可以考虑使用numarray模块。

> def fb(i,j):
>     ai = [-1 for x in range(i)]
>     array = [ai[:] for x in range(j)]
>     return array

这里的[:]则是必须的。

-- 
Qiangning Hong
http://www.hn.org/hongqn (RSS: http://feeds.feedburner.com/hongqn)

Registered Linux User #396996
Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id;=67907&t;=1>
Thunderbird! <http://www.spreadfirefox.com/?q=affiliates&id;=67907&t;=183>

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

2005年09月03日 星期六 09:03

Person person.lee at gmail.com
Sat Sep 3 09:03:53 HKT 2005

在 05-9-2,Qiangning Hong<hongqn at gmail.com> 写道:
> Person wrote:
> > 如果生成二维数组,我曾经用过两种方法。
> > 以前用fa函数,后来用fb函数,要比fa快几倍。
> >
> > def fa(i,j):
> >     array = [[-1 for x in range(i)][:] for y in range(j)]
> >     return array
> 
> 这个[:]是没有必要的,因为针对x的list comprehension会在每次y迭代时重新执行。
啊,这个[:]确实没有必要,多谢Qiangning提醒!
于是,写了个fc函数:
def fc(i,j):
   array = [[-1 for x in range(i)] for y in range(j)]
   return array
奇怪的是,profile结果显示fa与fc似乎速度不相上下。我们该如何解释呢?

import profile
profile.run('fc(100,50000)')
profile.run('fa(100,50000)')
profile.run('fc(100,50000)')
profile.run('fa(100,50000)')
结果:
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    4.054    4.054    4.054    4.054 test.py:23(fc)
        1    3.983    3.983    3.983    3.983 test.py:18(fa)
        1    4.042    4.042    4.042    4.042 test.py:23(fc)
        1    4.087    4.087    4.087    4.087 test.py:18(fa)

> 如果i, j比较大的话,用xrange替代range会有较大的性能提高。
> 如果数组内元素类型均为数值的话,可以考虑使用numarray模块。
我的系统上还没安装 Numarray,我想应该安装 一个。

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

2005年09月03日 星期六 11:13

Qiangning Hong hongqn at gmail.com
Sat Sep 3 11:13:33 HKT 2005

Person wrote:
>>>def fa(i,j):
>>>    array = [[-1 for x in range(i)][:] for y in range(j)]
>>>    return array
>>
> def fc(i,j):
>    array = [[-1 for x in range(i)] for y in range(j)]
>    return array
> 奇怪的是,profile结果显示fa与fc似乎速度不相上下。我们该如何解释呢?
> 
> import profile
> profile.run('fc(100,50000)')
> profile.run('fa(100,50000)')
> profile.run('fc(100,50000)')
> profile.run('fa(100,50000)')
> 结果:
>    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
>         1    4.054    4.054    4.054    4.054 test.py:23(fc)
>         1    3.983    3.983    3.983    3.983 test.py:18(fa)
>         1    4.042    4.042    4.042    4.042 test.py:23(fc)
>         1    4.087    4.087    4.087    4.087 test.py:18(fa)

差异还是有的,只是只占了极小部分的时间,很不明显罢了。用timeit模块看到快
了4%。
$ python -m timeit -s "i,j=100,50000" "[[-1 for x in range(i)][:] for y
in range(j)]"
10 loops, best of 3: 1.04 sec per loop
$ python -m timeit -s "i,j=100,50000" "[[-1 for x in range(i)] for y in
range(j)]"
10 loops, best of 3: 1 sec per loop

用xrange代替range,可以减少一部分时间,这时差异稍稍明显一些,快了6%。
$ python -m timeit -s "i,j=100,50000" "[[-1 for x in xrange(i)][:] for y
in xrange(j)]"
10 loops, best of 3: 946 msec per loop
$ python -m timeit -s "i,j=100,50000" "[[-1 for x in xrange(i)] for y in
xrange(j)]"
10 loops, best of 3: 890 msec per loop

当i,j比较小时,差异相当明显,快了14%。
$ python -m timeit -s "i,j=3, 5" "[[-1 for x in range(i)][:] for y in
range(j)]"
100000 loops, best of 3: 10.3 usec per loop
$ python -m timeit -s "i,j=3, 5" "[[-1 for x in range(i)] for y in
range(j)]"
100000 loops, best of 3: 8.87 usec per loop

*但是*,这些都不如进一步优化算法的提高大,用"*"运算符代替内层的list
comprehension,速度提高了89%。
$ python -m timeit -s "i,j=100,50000" "[[-1]*i for y in xrange(j)]"
10 loops, best of 3: 117 msec per loop


-- 
Qiangning Hong
http://www.hn.org/hongqn (RSS: http://feeds.feedburner.com/hongqn)

Registered Linux User #396996
Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id;=67907&t;=1>
Thunderbird! <http://www.spreadfirefox.com/?q=affiliates&id;=67907&t;=183>

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

2005年09月03日 星期六 11:31

Fluke fluke.l at gmail.com
Sat Sep 3 11:31:07 HKT 2005

我准备在python上搞web编程,有人能推荐python web编程和mod_python的资料吗?
我装mod_python的时候,在apache的httpd.conf里面设置的PythonHandler如果是test的话,
是不是我的python文件就一定要命名为test.py?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050903/995c5987/attachment.htm

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

2005年09月03日 星期六 11:39

Qiangning Hong hongqn at gmail.com
Sat Sep 3 11:39:22 HKT 2005

Fluke wrote:
> 我准备在python上搞web编程,有人能推荐python web编程和mod_python的资料吗?
> 我装mod_python的时候,在apache的httpd.conf里面设置的PythonHandler如果是
> test的话,是不是我的python文件就一定要命名为test.py?

DO NOT HIJACK OTHER'S THREAD!

你的问题和标题“请问,怎么声明一个二维的数组啊?”有什么关系吗?

问新问题,please post not reply.


-- 
Qiangning Hong
http://www.hn.org/hongqn (RSS: http://feeds.feedburner.com/hongqn)

Registered Linux User #396996
Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id;=67907&t;=1>
Thunderbird! <http://www.spreadfirefox.com/?q=affiliates&id;=67907&t;=183>

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号