我有10x10网格的一维位置列表:
[(0, 0), (0, 1), (0, 2), ..., (9, 9)]
我想像这样的numpy数组(10个长度列表的列表):
array([[ (0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9) ],
...,
[ (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9) ]])
如何在二维np数组中转换一维点列表?
我写了这个:
import numpy as np
l = []
for i in range(10):
for ii in range(10):
l.append((i, ii))
print(l)
a = np.array(l)
print(a)
a.shape = (a.size // 10, 10)
print(a)
但结果不是预期的结果:
python3.4 tmp/t.py
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), ..., (9, 9)]
[[0 0]
[0 1]
[0 2]
[0 3]
[0 4]
[0 5]
[0 6]
[0 7]
[0 8]
[0 9]
...
[9 9]]
[[0 0 0 1 0 2 0 3 0 4]
[0 5 0 6 0 7 0 8 0 9]
[1 0 1 1 1 2 1 3 1 4]
[1 5 1 6 1 7 1 8 1 9]
[2 0 2 1 2 2 2 3 2 4]
...
[9 0 9 1 9 2 9 3 9 4]
[9 5 9 6 9 7 9 8 9 9]]
答案 0 :(得分:3)
试试这个:
>>> z = [(i,j) for i in range(10) for j in range(10)]
>>> z
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), ..., (9, 9)]
>>> np.array(z).reshape((10,10, 2))
array([[[0, 0],
[0, 1],
[0, 2],
[0, 3],
[0, 4],
[0, 5],
[0, 6],
[0, 7],
[0, 8],
[0, 9]],
[[1, 0],
[1, 1],
[1, 2],
[1, 3],
[1, 4],
[1, 5],
[1, 6],
[1, 7],
[1, 8],
[1, 9]],
...
[[9, 0],
[9, 1],
[9, 2],
[9, 3],
[9, 4],
[9, 5],
[9, 6],
[9, 7],
[9, 8],
[9, 9]]])
答案 1 :(得分:1)
>>> array = np.array(range(10))
>>> array.reshape(5,2)
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
对于您的情况,如果您正在处理单个元素,则可以执行以下操作:
>>> >>> a = np.array(range(100))
>>> a.reshape(10,10)
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
但是,你的案例每个元素都有2个元素,所以你可以这样做:
>>> a = np.array([[i,j] for i in range(10) for j in range(10)])
>>> a.reshape(10,10, 2)
答案 2 :(得分:-1)
在我看来,大多数pythonic方式应该是这样的:
a = np.zeros(100,dtype=('i4,i4'))
a[:] = [(i,j) for i in range(10) for j in range(10)]
In [29]: a.reshape(10,10)
Out[29]:
array([[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7),
(0, 8), (0, 9)],
[(1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7),
(1, 8), (1, 9)],
[(2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7),
(2, 8), (2, 9)],
[(3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7),
(3, 8), (3, 9)],
[(4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7),
(4, 8), (4, 9)],
[(5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7),
(5, 8), (5, 9)],
[(6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7),
(6, 8), (6, 9)],
[(7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7),
(7, 8), (7, 9)],
[(8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7),
(8, 8), (8, 9)],
[(9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7),
(9, 8), (9, 9)]],
dtype=[('f0', '<i4'), ('f1', '<i4')])
编辑: 如果要保存元组,请选中:
{{1}}