0

I'm trying to do something like :

x = [1001, 0111, 1111]

I'm currently trying to do this as saving all values(decimal) in list arry and then trying to convert it to its binary notation using :

'{:04b}'.format(arry)

Stuck at how to add another element after i inserted one. I have tried to do using append() and + but append() is not going to work and using + will cause all the binary format to glue together, how i should achieve this. Thanks Edit :

 x  = []
 x  = '{:04b}'.format(10)
 print(x)
 k = '{:04b}'.format(2)
 x.append(k)

Gives err:

Traceback (most recent call last):
File "append.py", line 6, in <module>
 x.append(k)
AttributeError: 'str' object has no attribute 'append'
4
  • 1
    Show the code, append should work if you were appending to the list and not to the string. Commented Jan 19, 2016 at 3:27
  • Searching for the err occurred. But i'm appending to the list directly. Commented Jan 19, 2016 at 3:40
  • 1
    You're reassigning x to a string on the 2nd line of your example code. Commented Jan 19, 2016 at 3:40
  • Thanks got it. Oh i was neglecting that Commented Jan 19, 2016 at 3:42

1 Answer 1

2

When you insert the first element into the list, you should be using append for that as well:

x  = []
x.append('{:04b}'.format(10))
print(x)
x.append('{:04b}'.format(2))
print(x)

Alternatively, you could build the list in different ways... e.g. a list comprehension:

x = ['{:04b}'.format(y) for y in (10, 2)]

This has the advantage of scaling nicely to an arbitrary number of integers.


Edit (in response to comments):

If you really just want to format some numbers as binary inside a list, you can do it pretty easily using a simple class:

class BinFormatter(object):
    def __init__(self, v):
        self.v = v
    def __repr__(self):
        return '{:04b}'.format(self.v)

print([BinFormatter(v) for v in (10, 2)])

However, be aware that this goes against conventional norms regarding the __repr__ method which is generally supposed to provide the information necessary to reconstruct the object as much as is possible (in this case, the __repr__ method is dropping type information, so you wouldn't have enough information to reconstruct the object.)...

Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to get away with the initial ' and tailing ' as ['1010', '0010'] which is string can i get like [1010,0010] ??
@user2754673 -- That really depends on what you're looking for. Python won't print integers like that automatically. You could create a special class that represents itself like that, but it would be some work to get it to behave like an integer in any other respect... What are you going for here?
i just need it in [1000,0011] format. Actually was writing prog in C and printing binary was causing some problem as it was inside two for loops and model checker was unable to unwind as there were too many conversions required, so i was thinking to generate the same in python, i'm very unfamiliar with python. One thing i can do is generate ad then write script to get away with these ' ' . Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.