Convert numpy array to tuple

后端 未结 5 1674
北海茫月
北海茫月 2020-12-07 11:12

Note: This is asking for the reverse of the usual tuple-to-array conversion.

I have to pass an argument to a (wrapped c++) function as a nested tupl

5条回答
  •  情话喂你
    2020-12-07 11:32

    If you like long cuts, here is another way tuple(tuple(a_m.tolist()) for a_m in a )

    from numpy import array
    a = array([[1, 2],
               [3, 4]])
    tuple(tuple(a_m.tolist()) for a_m in a )
    

    The output is ((1, 2), (3, 4))

    Note just (tuple(a_m.tolist()) for a_m in a ) will give a generator expresssion. Sort of inspired by @norok2's comment to Greg von Winckel's answer

提交回复
热议问题