Обратный поиск dtype - PullRequest
       0

Обратный поиск dtype

0 голосов
/ 05 декабря 2018

Я хочу найти dtype для int, размер которого равен k.

Ожидаемое поведение

 >>> print(lookup(2))
 <class 'numpy.int16'>

 >>> print(lookup(4))
 <class 'numpy.int32'>

 >>> print(lookup(8))
 <class 'numpy.int64'>

, где lookup - это функция, которую я хочу знать.

1 Ответ

0 голосов
/ 05 декабря 2018

Вы можете использовать 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'>]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...