-3

I'm working on a project where I need to invert binary strings (i.e., change 0s to 1s and 1s to 0s). For example, if the input is "0110010", the output should be "1001101".

I'm looking for a solution that doesn't use Python loops (like for or while) for performance reasons. I've tried using a list comprehension, but I'm wondering if there's a more efficient way to do this.

Here's what I've tried so far:

s = "0110010"
s = "".join('1' if bit == '0' else '0' for bit in s)
print(s)

Is there a more efficient way to solve this problem in Python?

10
  • stackoverflow.com/questions/3920494/… there is an answer a few down the page using maketrans. Commented Sep 7, 2017 at 18:19
  • Why do you even have a billion-character string of ones and zeros? Commented Sep 7, 2017 at 18:19
  • this is a task. Commented Sep 7, 2017 at 18:21
  • and why people dont like this question? Commented Sep 7, 2017 at 18:22
  • What do you think it means to not use a loop? Commented Sep 7, 2017 at 18:23

4 Answers 4

4

The simple way by use swapping:

st = "0110010"
st=st.replace("0","a")
st=st.replace("1","0")
st=st.replace("a","1")
print (st)
Sign up to request clarification or add additional context in comments.

3 Comments

This is useful, but it requires 3 loops through the string. This can be done in one loop using the the comprehension idea I posted in a separate answer.
@RobertB: The comprehension is a Python-level loop, though, so it has a lot more overhead than the C-level loop inside replace. The comprehension doesn't actually win the timing.
Nice. Intuition proved me wrong... dang interpreter ;)
3

How to do this with either strings or binary literals.

Easy, use a bitwise operator. This is the proper way to do it, because it's useful to understand how to use these operators in Python. If this is a task, this seems like the way whoever set the task wanted it to be done.

# Make binary literal (0b prefix)
b = 0b11001001
# And use the complement operator
print(~b)

#Or if you want to print out the binary, try
print(bin(~b))

Here are some examples:

>>> ~0b110011
-52
>>> bin(~0b110001)
'-0b110010'
>>> bin(~0b11000)
'-0b11001'

If your binary number is a string:

s = "101"
# bin(~int(s,2)) = "010"

Note, you might want to trim the first three characters off of the space if you want just the binary characters, e.g.

bin(~int(s,2))[3:]

1 Comment

but i have strings, could you show ways how to convert string to bin , and bin to string?
1
''.join('1' if x == '0' else '0' for x in '0110010')

Comments

0

This piece of code works with strings, try it:

>>> from string import maketrans
>>>
>>> initial_string = "10101010100011010"
>>> converted_string = initial_string.translate(maketrans("10","01"))

>>> converted_string    # The result is
'01010101011100101'
>>>

Comments

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.