Multi-dimensional list initialization

wxjmfauth at gmail.com wxjmfauth at gmail.com
Wed Nov 7 09:57:29 CET 2012


Le mercredi 7 novembre 2012 02:55:10 UTC+1, Steven D'Aprano a écrit :

> 
> 
> 
> 
> 
> 
> Two-dimensional arrays in Python using lists are quite rare. Anyone who 
> 
> is doing serious numeric work where they need 2D arrays is using numpy, 
> 
> not lists. There are millions of people using Python, so it's hardly 
> 
> surprising that once or twice a year some newbie trips over this. But 
> 
> it's not something that people tend to trip over again and again and 
> 
> again, like C's "assignment is an expression" misfeature.
> 
> 

--------------------


>>> from vecmat6 import *
>>> from vmio5 import *
Traceback (most recent call last):
  File "<eta last command>", line 1, in <module>
ImportError: No module named vmio5
>>> from vmio6 import *
>>> from svdecomp6 import *
>>> mm = NewMat(3, 3)
>>> mm
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
>>> mm[0][0] = 1.0; mm[0][1] = 2.0; mm[0][2] = 3.0
>>> mm[1][0] = 11.0; mm[1][1] = 12.0; mm[1][2] = 13.0
>>> mm[2][0] = 21.0; mm[2][1] = 22.0; mm[2][2] = 23.0
>>> pr(mm, 'mm=')
mm=
(   1.00000e+000  2.00000e+000  3.00000e+000 )
(   1.10000e+001  1.20000e+001  1.30000e+001 )
(   2.10000e+001  2.20000e+001  2.30000e+001 )
>>> aa, b, cc = SVDecomp(mm)
>>> pr(aa, 'aa=')
aa=
(  -8.08925e-002 -9.09280e-001  4.08248e-001 )
(  -4.77811e-001 -3.24083e-001 -8.16497e-001 )
(  -8.74730e-001  2.61114e-001  4.08248e-001 )
>>> pr(b, 'b=')
b=
(   4.35902e+001  1.37646e+000  1.93953e-016 )
>>> pr(cc, 'cc=')
cc=
(  -5.43841e-001  7.33192e-001  4.08248e-001 )
(  -5.76726e-001  2.68499e-002 -8.16497e-001 )
(  -6.09610e-001 -6.79492e-001  4.08248e-001 )
>>> bb = VecToDiagMat(b)
>>> cct = TransposeMat(cc)
>>> oo = MatMulMatMulMat(aa, bb, cct)
>>> pr(oo, 'aa * bb * cct=')
aa * bb * cct=
(   1.00000e+000  2.00000e+000  3.00000e+000 )
(   1.10000e+001  1.20000e+001  1.30000e+001 )
(   2.10000e+001  2.20000e+001  2.30000e+001 )
>>>
>>> # or
>>> oo
[[0.9999999999999991, 1.9999999999999993, 2.9999999999999982],
[10.999999999999995, 11.99999999999999, 12.999999999999996],
[20.999999999999986, 21.999999999999975, 22.999999999999986]]



jmf



More information about the Python-list mailing list