【问题标题】:Changing an array of True and False answers to a hex value Python将 True 和 False 答案数组更改为十六进制值 Python
【发布时间】:2014-08-30 14:17:17
【问题描述】:

我有一个这样的正确和错误答案列表:

[True, True, True, False, False, True, False, False]
[True, True, False, False, True, False, False, True]
[True, False, False, True, False, False, True, True]
[False, False, True, False, False, True, True, True]
[False, True, False, False, True, True, True, False]
[True, False, False, True, True, True, False, False]
[False, False, True, True, True, False, False, True]
[False, True, True, True, False, False, True, False]

我想给 True 一个 1 值,给 False 一个 0 值,然后将该整体值转换为十六进制。 我该怎么做呢?我可以依次查看列表中的每个值,如果它等于“真”,则将该值更改为 1,如果它的“假”将值更改为 0,或者是否有更简单的方法可以将整个列表直接更改为十六进制?

编辑:这是 Pastebin 上的完整代码:http://pastebin.com/1839NKCx

谢谢

【问题讨论】:

    标签: python


    【解决方案1】:
    lists = [
        [True, True, True, False, False, True, False, False],
        [True, True, False, False, True, False, False, True],
        [True, False, False, True, False, False, True, True],
        [False, False, True, False, False, True, True, True],
        [False, True, False, False, True, True, True, False],
        [True, False, False, True, True, True, False, False],
        [False, False, True, True, True, False, False, True],
        [False, True, True, True, False, False, True, False],
    ]
    
    for l in lists:
        zero_one = map(int, l)  # convert True to 1, False to 0  using `int`
        n = int(''.join(map(str, zero_one)), 2)  # numbers to strings, join them
                                                 # convert to number (base 2)
        print('{:02x}'.format(n))  # format them as hex string using `str.format`
    

    输出:

    e4
    c9
    93
    27
    4e
    9c
    39
    72
    

    【讨论】:

    • @JohnZwinck,我认为你用字典对象覆盖了map。我在 Python 2.7 和 Python 3.4 中检查了这段代码。
    • “TypeError:map() 的参数 2 必须支持迭代” 对这究竟意味着什么感到困惑,Google 没有提供帮助。
    • @TomPitts,你能展示你的代码和完整的回溯吗? (通过放置像pastebin这样的地方)
    • @TomPitts,我认为您将非序列(或不可迭代)作为第二个参数传递。例如intmap(str, 1)。你应该通过可迭代:map(str, [1])
    • @TomPitts,map 没有电话。错误信息真的是运行源码来的吗?
    【解决方案2】:

    如果您想将一系列布尔值组合成一个值(作为位域),您可以执行以下操作:

    x = [True, False, True, False, True, False ]
    v = sum(a<<i for i,a in enumerate(x))
    print hex(v)
    

    【讨论】:

    • 这是一种享受,但它现在这样做:[True, True, True, False, False, True, False, False] 0x27 [True, True, False, False, True, False,假,真] 0x93 [真,假,假,真,假,假,真,真] 0xc9 [假,假,真,假,假,真,真,真] 0xe4 [假,真,假,假, True, True, True, False] 0x72 如何将所有这些十六进制值收集到一个字符串中?
    【解决方案3】:

    如果您使用reduce,则无需两步流程(假设MSB 照常在左侧):

    b = [True, True, True, False, False, True, False, False]
    val = reduce(lambda byte, bit: byte*2 + bit, b, 0)
    
    print val
    print hex(val)
    

    显示:

    228
    0xe4
    

    【讨论】:

    • 您可以只使用bit 而不是(1 if bit else 0),因为boolint 的子类,可用于算术运算。如果要明确,请使用int(bit)
    【解决方案4】:

    应该这样做:

    def bool_list_to_hex(list):
      n = 0
      for bool in list:
        n *= 2
        n += int(bool)
      return hex(n)
    

    【讨论】:

      【解决方案5】:

      单线:

      >>> lists = [
      [True, True, True, False, False, True, False, False],
      [True, True, False, False, True, False, False, True],
      [True, False, False, True, False, False, True, True],
      [False, False, True, False, False, True, True, True],
      [False, True, False, False, True, True, True, False],
      [True, False, False, True, True, True, False, False],
      [False, False, True, True, True, False, False, True],
      [False, True, True, True, False, False, True, False]]
      >>> ''.join(hex(int(''.join('1' if boolValue else '0' for boolValue in byteOfBools),2))[2:] for byteOfBools in lists)
      'e4c993274e9c3972'
      

      内连接产生一个由八个 0 和 1 组成的字符串。

      int(foo,2) 将字符串转换为将其解释为二进制的数字。

      hex 将其转换为十六进制格式。

      [2:] 从标准十六进制格式中删除前导“0x”

      外连接对所有子列表执行此操作,并且连接结果。

      【讨论】:

      • '%x' % int(''.join('01'[b] for b in sum(lists, [])), 2)
      • 当心,如果 8 个二进制数组值之一小于 10,这将给出不正确的结果。这是因为 hex() 为这些值返回单个字符,如 hex(1) == '1' 而不是 '01'。正如@falsetru 建议的那样,使用 '%x' 确实更好——实际上,您需要使用 '%02x' 来避免与上述答案相同的问题
      【解决方案6】:

      如果位列表超过 64,上述所有方法都不起作用。 还可以讨论在转换为十六进制之前多次转换布尔值是否有效,尤其是字符串。

      这是一个提案,MSB 在位列表的左侧:

      from collections import deque
      
      # (lazy) Padd False on MSB side so that bitlist length is multiple of 4.
      # Padded length can be zero
      msb_padlen = (-len(bitlist))%4
      bitlist = deque(bitlist)
      bitlist.extendleft([False]*msb_padlen)
      
      # (lazy) Re-pack list of bits into list of 4-bit tuples
      pack4s = zip(* [iter(bitlist)]*4)
      
      # Convert each 4-uple into hex digit
      hexstring = [hex(sum(a<<i for i,a in enumerate(reversed(pack4))))[-1] for pack4 in pack4s ]
      
      # Compact list of hex digits into a string
      hexstring = '0x'+''.join(hexstring)
      

      4 位元组 pack4 为 (msb,...,lsb) => 计算相应整数时必须取反。

      替代方案:

      hexstring = [hex(sum(a<<3-i for i,a in enumerate(pack4)))[-1] for pack4 in pack4s ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 2021-09-29
        • 1970-01-01
        • 2020-02-19
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多