【问题标题】:Print an integer array as hexadecimal numbers将整数数组打印为十六进制数
【发布时间】:2012-02-25 21:02:11
【问题描述】:

我有一个使用创建的数组

array1 = np.array([[25,  160,   154, 233],
                   [61, 244,  198,  248],
                   [227, 226, 141, 72 ],
                   [190, 43,  42, 8]],np.int) ;

显示为

[[25,  160, 154, 233]
 [61,  244, 198, 248]
 [227, 226, 141,  72]
 [190,  43,  42 ,  8]]

如何将此数组显示为十六进制数字,如下所示:

[[0x04,  0xe0,  0x48, 0x28]
 [0x66,  0xcb,  0xf8, 0x06]
 [0x81,  0x19,  0xd3, 0x26]
 [0xe5,  0x9a,  0x7a, 0x4c]]

注意:十六进制数字可能不是整数数字的真正转换。我已经填充了十六进制数组,只是为了举例说明我需要什么。

【问题讨论】:

  • 你使用的是什么版本的 numpy (np.version.version)?

标签: python arrays numpy


【解决方案1】:

如果您正在寻找的只是为了展示,您可以执行以下操作:

>>> a = [6, 234, 8, 9, 10, 1234, 555, 98]
>>> print '\n'.join([hex(i) for i in a])
0x6
0xea
0x8
0x9
0xa
0x4d2
0x22b
0x62

【讨论】:

    【解决方案2】:

    Python 有一个内置的十六进制函数,用于将整数转换为其十六进制表示(字符串)。您可以使用 numpy.vectorize 将其应用于多维数组的元素。

    >>> import numpy as np
    >>> A = np.array([[1,2],[3,4]])
    >>> vhex = np.vectorize(hex)
    >>> vhex(A)
    array([['0x1', '0x2'],
           ['0x3', '0x4']], 
          dtype='<U8')
    

    如果速度是一个问题,可能有一个内置的方法可以使用 numpy 执行此操作,这将是一个更好的选择。

    【讨论】:

    • -0,由于numpy.set_printoptions的存在。
    • 当只需要偶尔的十六进制字符串时,矢量化很方便。否则需要 original=get_printoptions(), set_printoptions(hex), print hex, set_printoptions(original)
    【解决方案3】:

    您可以设置 numpy 的打印选项来执行此操作。

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

    给予

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

    最后的 L 只是因为我在 64 位平台上,它正在向格式化程序发送 long。要解决此问题,您可以使用

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

    【讨论】:

    • OP 仅供参考。 set_printoptionsformatter 参数出现在 2.0 文档中,但不在 1.6 中。所以我想这是一个新功能。如果你有一个旧版本,你可以滚动你自己的格式化函数,并使用numpy.set_string_function 将它提供给 numpy。您提供的函数需要格式化 整个数组 而不是像贾斯汀的答案中的单个项目。
    • 对于像我这样不希望在全局范围内更改打印选项的人,现在有一个 numpy.printoptions 辅助方法,您可以像这样使用:with np.printoptions(formatter={'int':hex}):
    【解决方案4】:

    这个单线应该可以完成这项工作:

    print '[' + '],\n['.join(','.join(hex(n) for n in ar) for ar in array1) + ']'
    

    【讨论】:

    • -0,由于numpy.set_printoptions的存在。
    • 是的,这绝对是一个更好的解决方案。提问者,请参阅 Justin Peel 的回答,不要使用我的。
    • Numpy 绝对是更好的解决方案!如果您可以访问 numpy。
    【解决方案5】:

    只要投入我的两分钱,如果它始终是这样的二维数组,您可以非常简单地使用列表推导来做到这一点

    a = [[1,2],[3,4]]
    print [map(hex, l) for l in a]
    

    给你[['0x1', '0x2'], ['0x3', '0x4']]

    【讨论】:

      【解决方案6】:

      应该可以通过numpy.set_printoptions 获得您想要的行为,使用formatter 关键字arg。它需要一个类型规范的字典(即'int')作为键和一个可调用的对象,返回要打印的字符串。我会插入代码,但我的旧版本 numpy 还没有该功能。 (呃。)

      【讨论】:

        【解决方案7】:

        我正在使用矢量化 np.base_repr,因为我需要用填充的 0 调整结果

        import numpy as np
        
        width = 4
        base = 16
        array1 = np.array([[25,  160,   154, 233],
                           [61, 244,  198,  248],
                           [227, 226, 141, 72 ],
                           [190, 43,  42, 8]],np.int)
        
        
        base_v = np.vectorize(np.base_repr)
        padded = np.char.rjust(base_v(array1, base), width, '0')
        result = np.char.add('0x', padded)
        

        输出:

        [['0x0019' '0x00A0' '0x009A' '0x00E9']
         ['0x003D' '0x00F4' '0x00C6' '0x00F8']
         ['0x00E3' '0x00E2' '0x008D' '0x0048']
         ['0x00BE' '0x002B' '0x002A' '0x0008']]
        

        【讨论】:

          【解决方案8】:
          array1_hex = np.array([[hex(int(x)) for x in y] for y in array1])
          print array1_hex
          # => array([['0x19', '0xa0', '0x9a', '0xe9'],
          #           ['0x3d', '0xf4', '0xc6', '0xf8'],
          #           ['0xe3', '0xe2', '0x8d', '0x48'],
          #           ['0xbe', '0x2b', '0x2a', '0x8']], 
          #          dtype='|S4')
          

          【讨论】:

          • 此答案失败并出现错误:TypeError: 'int' object is not iterable
          猜你喜欢
          • 2016-06-17
          • 1970-01-01
          • 2013-12-13
          • 1970-01-01
          • 1970-01-01
          • 2014-09-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多