Take this code:
import itertools as it
import numpy as np
data = ['a','b','c','d']
dw = np.array([1, 3], dtype=np.int64)
print(list(it.islice(data,dw[0],dw[1],1)))
On Python 2.7 it prints ['b', 'c',] as expected.
On Python 3.6 it throws an exception:
ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.
The same goes for np.int32, and other methods of the itertools package throw similar errors, e.g. when you use permutations you get TypeError: Expected int as r.
I couldn't find much on this apart from this numpy issue and related ones, but that one was closed 3 years ago implying it was solved.
And basic things like indexing with numpy ints data[dw[0]] or boolean comparisons like dw[0] == 1 work just fine.
Am I missing something? Could this be a Python 3 bug?
解决方案
a numpy.int64 is apparently not a subclass of int
a, b = dw[0], dw[1]
type(a)
numpy.int64
isinstance(a, int)
False
Numpy documentation
The documentation mentions this explicitly
Warning
The int_ type does not inherit from the int built-in under Python 3,
because type int is no longer a fixed-width integer type.
Solution
print(list(it.islice(data, int(dw[0]) , int(dw[1]), 1)))
or numpy slicing
data[dw[0]:dw[1]:1]