当我将[::-1]与变量一起使用时会发生什么?

问题描述

所以我有这个代码:

t=int(input())
while t:
 s=int(input())
 n=bin(s)
 n=n[2:][::-1]
 if n.count('1')==1:
  pos=n.find('1')+1
  print(pos)
else:
  print('-1')
t-=1

我想确切知道此行中发生了什么:

 n=n[2:][::-1]

[::-1]是什么意思?

解决方法

以n的二进制值的倒数开头,其中不包括“ 0b”值。例如,如果输入6作为n的值。二进制值为0b110,不包括0b的反向值为011。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...