Exemples de comment inverser l'ordre des lignes d'un tableau en python avec numpy. Soit un tableau quelconque:
>>> import numpy as np>>> table = np.array([[0., 4., 4., 2., 7., 0.],... [1., 2., 7., 2., 3., 4.],... [2., 2., 3., 3., 6., 3.],... [3., 9., 0., 6., 3., 3.],... [4., 3., 1., 8., 2., 5.],... [5., 9., 3., 7., 6., 8.],... [6., 9., 5., 8., 1., 8.],... [7., 9., 6., 7., 3., 7.],... [8., 2., 1., 4., 4., 8.],... [9., 3., 2., 6., 0., 0.]])
\begin{equation}
\begin{array}{cccccc}
0. & 4. & 4. & 2. & 7. & 0. \\
1. & 2. & 7. & 2. & 3. & 4. \\
2. & 2. & 3. & 3. & 6. & 3. \\
3. & 9. & 0. & 6. & 3. & 3. \\
4. & 3. & 1. & 8. & 2. & 5. \\
5. & 9. & 3. & 7. & 6. & 8. \\
6. & 9. & 5. & 8. & 1. & 8. \\
7. & 9. & 6. & 7. & 3. & 7. \\
8. & 2. & 1. & 4. & 4. & 8. \\
9. & 3. & 2. & 6. & 0. & 0.
\end{array}
\end{equation}
Pour inverser l'ordre des lignes du tableau on peut utiliser la fonction numpy flipud()
>>> table = np.flipud(table)>>> tablearray([[9., 3., 2., 6., 0., 0.],[8., 2., 1., 4., 4., 8.],[7., 9., 6., 7., 3., 7.],[6., 9., 5., 8., 1., 8.],[5., 9., 3., 7., 6., 8.],[4., 3., 1., 8., 2., 5.],[3., 9., 0., 6., 3., 3.],[2., 2., 3., 3., 6., 3.],[1., 2., 7., 2., 3., 4.],[0., 4., 4., 2., 7., 0.]])>>>
\begin{equation}
\begin{array}{cccccc}
9. & 3. & 2. & 6. & 0. & 0.\\
8. & 2. & 1. & 4. & 4. & 8.\\
7. & 9. & 6. & 7. & 3. & 7.\\
6. & 9. & 5. & 8. & 1. & 8.\\
5. & 9. & 3. & 7. & 6. & 8.\\
4. & 3. & 1. & 8. & 2. & 5.\\
3. & 9. & 0. & 6. & 3. & 3.\\
2. & 2. & 3. & 3. & 6. & 3.\\
1. & 2. & 7. & 2. & 3. & 4.
\end{array}
\end{equation}
Références
| Liens | Site |
|---|---|
| numpy.flipud | docs.scipy.org |
| numpy.flipud() in Python | geeksforgeeks.org |
| Python numpy.flipud() Examples | programcreek.com |
| Most efficient way to reverse a numpy array | stackoverflow |
| numpy.loadtxt | docs.scipy.org |
