解决Python/IPython中的TypeError
我有以下功能的问题解决Python/IPython中的TypeError,python,python-3.x,python-2to3,Python,Python 3.x,Python 2to3,我有以下功能的问题 def get_lexographically_next_bit_sequence(self, bits): """ Bit hack from here: http://www-graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation Generator even does this in poker order rank so no need to sort
def get_lexographically_next_bit_sequence(self, bits):
"""
Bit hack from here:
http://www-graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation
Generator even does this in poker order rank
so no need to sort when done! Perfect.
"""
t = (bits | (bits - 1)) + 1
next = t | ((((t & -t) // (bits & -bits)) >> 1) - 1)
yield next
while True:
t = (next | (next - 1)) + 1
next = t | ((((t & -t) // (next & -next)) >> 1) - 1)
yield next
此函数返回以下错误:
TypeError:不支持>>的操作数类型:“float”和“int”
注:
这个python库仅在2.7中受支持,为了使用它,我使用了2to3。图书馆的其他部分按预期工作,所以我一般相信2to3工作正常
我正试图在IPython 3.5中运行它,我听说类似这样的错误可能会在IPython中发生,所以我想知道这是否与此相关 问题产生于您试图在两种不同的数据类型(
float
和int
)之间执行二进制右移(>>)
)。用(int(((t&-t)/(next&-next))>>1)-1将浮点值向下转换为int应该就我所知是可行的。我用10101测试了代码,效果很好。有问题的输入是什么?@MHornbacher bits=31my results:Windows 10 1607 python 2.7.3和python 3.6.0您的代码在3秒钟内不会返回任何错误。但是,
的任何输入都不可能是浮点数。你不能从&
中获得浮点数,/
不会给出浮点数,除非你给出浮点数。我想他给出了浮点数。通过31的测试用例与原始代码一起工作。浮点无法到达此函数中的任何>
运算符。然后我猜错误被抛出到其他地方。既然DMS什么也没说,我想它是固定的。