Print an integer array as hexadecimal numbers

后端 未结 7 510
南方客
南方客 2020-12-11 00:43

I have an array created by using

array1 = np.array([[25,  160,   154, 233],
                   [61, 244,  198,  248],
                   [227, 226, 141, 72 ]         


        
7条回答
  •  忘掉有多难
    2020-12-11 01:10

    You can set the print options for numpy to do this.

    import numpy as np
    np.set_printoptions(formatter={'int':hex})
    np.array([1,2,3,4,5])
    

    gives

    array([0x1L, 0x2L, 0x3L, 0x4L, 0x5L])
    

    The L at the end is just because I am on a 64-bit platform and it is sending longs to the formatter. To fix this you can use

    np.set_printoptions(formatter={'int':lambda x:hex(int(x))})
    

提交回复
热议问题