python numpy将两个不同的维度数组转换为一个元组

时间:2013-10-09 11:36:38

标签: python arrays numpy tuples

我有两个数组,一个是(array A[10][10000]):

 1:   [[   0    0    0 ...,  156  665  621]
 2:    [   0    0    0 ..., -187 -186 -186]
 3:    [   0    0    0 ...,   61  -22  -55]
       ..., 
 8:    [   0    0    0 ...,  540  402  496]
 9:    [   0    0    0 ...,   31   31   33]
10:    [   0    0    0 ..., -525 -504 -492]]

长度为10*10000,类型为<type 'numpy.ndarray'>,dtype为int16

另一个是(array B[10]):b=numpy.arange(10)

[   0    1    2 ..., 7 8 9]

长度为10,类型为<type 'numpy.ndarray'>,dtype为int32

我希望将这两个不同的维数组转换为一个这样的元组(tuple C):

(array([[ 0,  0,  0, ...,  156,  665,  621],
        [ 0,  0,  0, ..., -187, -186,    0],
        [ 0,  0,  0, ...,   61,  -22,  -55],
        ..., 
        [ 0,  0,  0, ...,  540,  402,  496],
        [ 0,  0,  0, ...,   31,   31,   33],
        [ 0,  0,  0, ..., -525, -504, -492]], dtype=int16),
 array( [ 0,  1,  2, ...,    7,    8,    9], dtype=int32))

有关元组C的更多信息:

print A[0].shape = (10, 10000)
print A[0].dtype.name = int16
print type(A[0]) = <type 'numpy.ndarray'>

print A[1].shape  = (10,)
print A[1].dtype.name = int32
print type(A[1]) = <type 'numpy.ndarray'>

1 个答案:

答案 0 :(得分:2)

除非我遗漏了某些东西,否则你只需要一个以两个数组为元素的元组:

C = (A, B)