0

I have a list and each number in list represent exponents of powers of 2 For example, I have a list

 Binarylist = [[4,3,0],[2,1,0]]

for the Binarylist[0], each number can be represented as 2^4, 2^3, 2^0 thus 11001 in base 2.

for the Binarylist[1], each number can be represented as 2^2, 2^1, 2^0 thus 111 in base 2.

How can i make [[4,3,0],[2,1,0]] to 11001 and 111?

user2848250
  • 35
  • 1
  • 1
  • 3
  • 3
    A few clarifying questions: Is your question "how do the lists represent these powers of two?" or "how do I convert the lists into the binary numbers?" Are the lists always sorted? Do you want the number corresponding to the binary number 11001, the decimal number 11001, or the string 11001? – templatetypedef Oct 17 '13 at 01:22

6 Answers6

3

There are two parts to a solution to your problem. First you need to convert your lists into integers, since the numbers in the list represent the ones in a binary string. Then you can use bin to convert to binary format:

>>> bin(sum(2**n for n in Binarylist[0]))
'0b11001'

If you want more control of how the integer is converted into a string in binary format, you can consult this SO post.

Community
  • 1
  • 1
mdml
  • 22,442
  • 8
  • 58
  • 66
2

Your indices represent the digits into a binary number. So, assuming the first number in the list is always the largest power:

def to_ones_repr(powers_repr):
    res = [0] * (powers_repr[0] + 1)
    for power in powers_repr:
        res[-(power+1)] = 1
    return res

Usage:

>>> to_ones_repr([4, 3, 0])
[1, 1, 0, 0, 1]
>>> to_ones_repr([2, 1, 0])
[1, 1, 1]

Or if you want a string do this instead:

def to_ones_str_repr(powers_repr):
    res = ['0'] * (powers_repr[0] + 1)
    for power in powers_repr:
        res[-(power+1)] = '1'
    return "".join(res)

>>> to_ones_str_repr([4, 3, 0])
'11001'
>>> to_ones_str_repr([2, 1, 0])
'111'

Alternatively, calculate the number and then use the bin function:

def to_ones_str_repr(powers_repr):
    return bin(sum(2**i for i in powers_repr))

>>> to_ones_str_repr([4, 3, 0])
'0b11001'
>>> to_ones_str_repr([2, 1, 0])
'0b111'
Claudiu
  • 224,032
  • 165
  • 485
  • 680
2

This will give you a list of strings, one for each list of exponents in Binarylist.

[format(sum(2**n for n in powers), 'b') for powers in Binarylist]

Result:

['11001', '111']
martineau
  • 119,623
  • 25
  • 170
  • 301
1

So number in each list are indexes into binary number where bit is set to 1 all other places are 0, so just iterate from max index to 0 and output 1 if index is in list else 0

>>> l=[4,3,0]
>>> [1 if i in l else 0 for i in range(max(l),-1,-1)]
[1, 1, 0, 0, 1]

To make it more efficient you can use set instead of list so that i in l is constant time, and you can join the result list to output string if you need.

Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • Thats not a binary number, its an array of integers. –  Oct 17 '13 at 01:30
  • This is all a matter of representations anyway. OP asked for "11001", which is also not a binary number. He didn't ask for 0b11001…. It's not clear that this answer is wrong, though OP could make it clearer. – kojiro Oct 17 '13 at 01:32
1

As you want to do a bitwise operation, why not use the "bitwise or" operator?

import operator

a = [4,3,0]
mynum = reduce(operator.or_, (2**x for x in a))
print bin(mynum)

The reduce expression is the equivalent of 2**4 | 2**3 | 2**0

nakedfanatic
  • 3,108
  • 2
  • 28
  • 33
  • `operator.or_` is the better way to do it, but if one really wanted to skip the import, then `int.__xor__` would work just as well... I'd probably also go for `format(mynum, 'b')` instead of `bin`... – Jon Clements Oct 17 '13 at 01:33
  • Good point @JonClements. `int.__or__` is also available. I have no idea whether it would be a better choice than `int.__xor__`, but both would work for this problem. – nakedfanatic Oct 17 '13 at 01:44
0

Like this:

[bin(sum(map(lambda y: pow(2,y) ,x))) for x in BinaryList]

Which gives:

['0b11001', '0b111']

This uses a list comprehension to iterate through each list in the list. Then finds the binary value of the sum of when mapping the power function to each number in x.

As Claudiu mentions, this can be done using two list comprehensions, and no map/lambda which is probably easier to read and definitely shorter:

[bin(sum([pow(2,y) for y in x])) for x in BinaryList]
  • Should really use a list comprehension instead of `map` with `lambda`. – Claudiu Oct 17 '13 at 01:30
  • @Claudiu added the alternate way, whats the benefit if I may ask? –  Oct 17 '13 at 01:33
  • 1
    It's actually a good amount faster with the list comprehension. With `map` and `lambda` you create a function and call it for each item, whereas with a list comprehension the code gets executed without a function call. Plus it is more readable + pythonic, `map(lambda ITEM: OP, LIST)` vs `[OP for ITEM in LIST]`. I think `map` is more readable if I already have a function, though, e.g. `map(str.strip, l)` instead of `[str.strip(x) for x in l]`. – Claudiu Oct 17 '13 at 01:35