75593
①Matrix→list変換を教えて下さい。  
②Matrix→list変換。list→Matrix変換。それぞれ1行で書けますか？  
よろしくお願いします。  
(参考)  
[sympyのMatrix.row_insertの使い方を教えて下さい。](https://ja.stackoverflow.com/questions/75475/sympy%e3%81%aematrix-
row-
insert%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%e3%82%92%e6%95%99%e3%81%88%e3%81%a6%e4%b8%8b%e3%81%95%e3%81%84)

```

    from sympy import *
    mylist=[[0]*3]*3
    mylist[0]=[-4, 1, 9]
    mylist[1]=[1, 2, -4]
    mylist[2]=[2, -3, 6]
    myMatrix=Matrix()
    for i in range(len(mylist)):
        myMatrix = myMatrix.row_insert(i, Matrix([mylist[i]]))
    print("# mylist  ",type(mylist))
    print("# mylist  ",len(mylist),mylist)
    print("# myMatrix",type(myMatrix))
    print("# myMatrix",len(myMatrix),myMatrix)
    mylist2=[[0]*3]*3
    for i in range(sqrt(len(myMatrix))):
         for j in range(sqrt(len(myMatrix))):
              # print(myMatrix[i,j])
              mylist2[i][j] = myMatrix[i, j]
    print("# mylist2 ",type(mylist2))
    print("# mylist2 ",len(mylist2),mylist2)
    # mylist   <class 'list'>
    # mylist   3 [[-4, 1, 9], [1, 2, -4], [2, -3, 6]]
    # myMatrix <class 'sympy.matrices.dense.MutableDenseMatrix'>
    # myMatrix 9 Matrix([[-4, 1, 9], [1, 2, -4], [2, -3, 6]])
    # mylist2  <class 'list'>
    # mylist2  3 [[2, -3, 6], [2, -3, 6], [2, -3, 6]]
    # mylist2がおかしいです。
    
```
CC BY-SA 4.0
2021-04-27T09:51:04.287
75536
2021-04-30T11:05:55.280
17199
question
python sympy
sympyでlistとMatrixの相互変換をやってみました。Matrix→list変換を教えて下さい。
(参考)【Python】多次元配列に値を代入するときの注意点  
<https://qiita.com/oyoshi0022/items/7475951f465d20ad4970>

次のコードの5行目mylist=[[0]*3]*3が、あやしいと思います。

```

    from sympy import *
    var('a11 a12 a13')
    var('a21 a22 a23')
    var('a31 a32 a33')
    mylist=[[0]*3]*3
    mylist[0]=[a11, a12, a13]
    mylist[1]=[a21, a22, a23]
    mylist[2]=[a31, a32, a33]
    myMatrix=Matrix()
    for i in range(len(mylist)):
        myMatrix = myMatrix.row_insert(i, Matrix([mylist[i]]))
    print("# mylist  ",type(mylist))
    print("# mylist  ",len(mylist),mylist)
    print("# myMatrix",type(myMatrix))
    print("# myMatrix",len(myMatrix),myMatrix)
    # mylist2=[[0]*3 for i in range(3)]
    mylist2=[[0]*len(mylist) for i in range(len(mylist))]
    for i in range(len(mylist)):
         for j in range(len(mylist)):
              mylist2[i][j] = myMatrix[i, j]
    print("# mylist2 ",type(mylist2))
    print("# mylist2 ",len(mylist2),mylist2)
    # mylist   <class 'list'>
    # mylist   3 [[a11, a12, a13], [a21, a22, a23], [a31, a32, a33]]
    # myMatrix <class 'sympy.matrices.dense.MutableDenseMatrix'>
    # myMatrix 9 Matrix([[a11, a12, a13], [a21, a22, a23], [a31, a32, a33]])
    # mylist2  <class 'list'>
    # mylist2  3 [[a11, a12, a13], [a21, a22, a23], [a31, a32, a33]]
    
```
CC BY-SA 4.0
2021-04-30T11:05:55.280
75593
2021-04-30T11:05:55.280
17199
75536
answer