如何在python中反转所有位?

Aka*_*bey 0 python math binary bit

我想按如下方式转换这些二进制表示。

"1111" -> "0000"
"1010" -> "0101"
Run Code Online (Sandbox Code Playgroud)

小智 6

希望你看起来像这样,

def convert(inp):
    return ''.join(['1','0'][int(i)] for i in inp)
convert('1010')
Run Code Online (Sandbox Code Playgroud)

输出

0101
Run Code Online (Sandbox Code Playgroud)