Computer >> Máy Tính >  >> Lập trình >> Python

Trả về kiểu vô hướng có độ chính xác cao nhất cùng loại với kiểu đầu vào trong Python

Để trả về kiểu vô hướng có độ chính xác cao nhất cùng loại với đầu vào, hãy sử dụng phương thức themaximum_sctype () trong Python Numpy. Tham số là kiểu dữ liệu đầu vào. Đây có thể là đối tượng adtype hoặc một đối tượng có thể chuyển đổi thành dtype.

Các bước

Đầu tiên, hãy nhập thư viện được yêu cầu -

import numpy as np

Sử dụng phương thức Maximum_sctype () trong Numpy. Nhận loại vô hướng có độ chính xác cao nhất -

print("Result...",np.maximum_sctype(int))
print("Result...",np.maximum_sctype(np.uint8))
print("Result...",np.maximum_sctype(complex))
print("Result...",np.maximum_sctype(str))
print("Result...",np.maximum_sctype('f4'))
print("Result...",np.maximum_sctype('i2'))
print("Result...",np.maximum_sctype('i4'))
print("Result...",np.maximum_sctype('i8'))

Ví dụ

import numpy as np

# To return the scalar type of highest precision of the same kind as the input, use the maximum_sctype() method in Python Numpy
# The parameter is the input data type. This can be a dtype object or an object that is convertible to a dtype.
print("Using the maximum_sctype() method in Numpy\n")

# Get the scalar type of highest precision
print("Result...",np.maximum_sctype(int))
print("Result...",np.maximum_sctype(np.uint8))
print("Result...",np.maximum_sctype(complex))
print("Result...",np.maximum_sctype(str))
print("Result...",np.maximum_sctype('f4'))
print("Result...",np.maximum_sctype('i2'))
print("Result...",np.maximum_sctype('i4'))
print("Result...",np.maximum_sctype('i8'))

Đầu ra

Using the maximum_sctype() method in Numpy

Result... <class 'numpy.int64'>
Result... <class 'numpy.uint64'>
Result... <class 'numpy.complex256'>
Result... <class 'numpy.str_'>
Result... <class 'numpy.float128'>
Result... <class 'numpy.int64'>
Result... <class 'numpy.int64'>
Result... <class 'numpy.int64'>