简体   繁体   中英

How to convert list of list binary into list of decimal?

LIST OF LIST BIN DIVIDED INTO 8: [[0, 1, 1, 0, 0, 1, 0, 1], [0, 1, 1, 1, 0, 1, 1, 1]]

the output I want is:

[101, 119]

This is more complex but significantly faster than any kind of string manipulation as it's essentially just integer arithmetic.

from timeit import timeit

lob = [[0, 1, 1, 0, 0, 1, 0, 1], [0, 1, 1, 1, 0, 1, 1, 1]]

def v1():
    result = []
    for e in lob:
        r = 0
        for _e in e:
            r = r * 2 + _e
        result.append(r)
    return result

def v2():
    return [int(''.join([str(y) for y in x]), 2) for x in lob]

assert v1() == v2()

for func in v1, v2:
    print(func.__name__, timeit(func))

Output:

v1 0.6906622060014342
v2 2.173182999999881

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM