📜  在Python中反转二进制位的不同方法

📅  最后修改于: 2022-05-13 01:55:16.886000             🧑  作者: Mango

在Python中反转二进制位的不同方法

我们知道数字的二进制值是什么样子的。例如,10(数字十)的二进制值是 1010(二进制值)。

有时需要反转位,即,0 到 1(零到一)和 1 到 0(一到零)。以下是我们可以在Python中反转位的几种方法。

1)使用循环:通过迭代每个位,我们可以将位 1 更改为位 0,反之亦然。

Python3
bit_s = '1010'
inverse_s = ''
  
for i in bit_s:
    
    if i == '0':
        inverse_s += '1'
          
    else:
        inverse_s += '0'
          
print("Inversed string is ",
      inverse_s)


Python3
# create a dictionary
b_dict = {'0': '1', '1': '0'}
  
bit_s = '1010'
  
inverse_s = ''
  
for i in bit_s:
    
    inverse_s += b_dict[i]
      
print("Inversed string is",
      inverse_s)


Python3
bit_s = '1010'
  
# using ternary operator with 
# list comprehension
inverse_s = ''.join(['1' if i == '0' else '0'
                     for i in bit_s])
  
print("Inversed string is", 
      inverse_s)


Python3
bit_s = '1010'
  
# replace "1" with "2" 
# output : "2020"
inverse_s = bit_s.replace('1', '2')
  
# replace "0" with "1" 
# output : "2121"
inverse_s = inverse_s.replace('0', '1')
  
# replace "0" with "1" 
# output : "0101"
inverse_s = inverse_s.replace('2', '0')
  
print("Inversed string is", 
      inverse_s)


Python3
bit_s = '1010'
  
# convert binary string 
# into integer
temp = int(bit_s, 2)
  
# applying Ex-or operator
# b/w 10 and 31
inverse_s = temp ^ (2 ** (len(bit_s) + 1) - 1)
  
# convert the integer result 
# into binary result and then 
# slicing of the '0b1' 
# binary indicator
rslt = bin(inverse_s)[3 : ]
  
# print the result
print("Inversed string is", 
      rslt )


输出:

Inversed string is 0101

2)使用字典:字典在访问元素方面非常快,它需要 O(1) 时间复杂度。

Python3

# create a dictionary
b_dict = {'0': '1', '1': '0'}
  
bit_s = '1010'
  
inverse_s = ''
  
for i in bit_s:
    
    inverse_s += b_dict[i]
      
print("Inversed string is",
      inverse_s)

输出:

Inversed string is 0101

3)使用列表推导:列表推导是访问、添加、操作列表的简写符号。

Python3

bit_s = '1010'
  
# using ternary operator with 
# list comprehension
inverse_s = ''.join(['1' if i == '0' else '0'
                     for i in bit_s])
  
print("Inversed string is", 
      inverse_s)

输出:

Inversed string is 0101

4)使用字符串的replace() 方法:在Python中,字符串有一个内置方法,即字符串.replace(existing_characters, new_characters),它将所有existing_characters 替换为new_characters。

Python3

bit_s = '1010'
  
# replace "1" with "2" 
# output : "2020"
inverse_s = bit_s.replace('1', '2')
  
# replace "0" with "1" 
# output : "2121"
inverse_s = inverse_s.replace('0', '1')
  
# replace "0" with "1" 
# output : "0101"
inverse_s = inverse_s.replace('2', '0')
  
print("Inversed string is", 
      inverse_s)

输出:

Inversed string is 0101

5)使用按位异或运算符:异或运算符如果其中一位为 1 且其他位为 0,则返回 1,否则返回 false。

Python3

bit_s = '1010'
  
# convert binary string 
# into integer
temp = int(bit_s, 2)
  
# applying Ex-or operator
# b/w 10 and 31
inverse_s = temp ^ (2 ** (len(bit_s) + 1) - 1)
  
# convert the integer result 
# into binary result and then 
# slicing of the '0b1' 
# binary indicator
rslt = bin(inverse_s)[3 : ]
  
# print the result
print("Inversed string is", 
      rslt )

输出:

Inversed string is  0101