反向查找dtype

时间:2018-12-05 02:44:22

标签: numpy

我想找到项大小为k的if __name__ == '__main__': #main() window = Tk() window.title("Jukebox") initiateJukebox = JukeboxContent() initiateJukebox.directoryChooser() constructGUI = JukeboxGUI(window) constructGUI.constructButtons() constructGUI.nextButton.bind("<Button-1>", nextSong) constructGUI.previousButton.bind("<Button-1>", previousSong) constructGUI.stopButton.bind("<Button-1>", stopSong) window.mainloop() 的dtype。

预期行为

int

>>> print(lookup(2)) <class 'numpy.int16'> >>> print(lookup(4)) <class 'numpy.int32'> >>> print(lookup(8)) <class 'numpy.int64'> 是我想知道的功能。

1 个答案:

答案 0 :(得分:2)

您可以使用np.sctypes

lookup = {t().itemsize: t for t in np.sctypes['int']}

lookup
# {1: <class 'numpy.int8'>, 2: <class 'numpy.int16'>, 4: <class 'numpy.int32'>, 8: <class 'numpy.int64'>}

或使用np.sctypeDict和f字符串:

def lookup(n):
    return np.sctypeDict[f'i{n}']

[lookup(n) for n in (1, 2, 4, 8)]
# [<class 'numpy.int8'>, <class 'numpy.int16'>, <class 'numpy.int32'>, <class 'numpy.int64'>]