一站式解决Python中所有类型转换问题,支持标准类型与第三方库类型的无缝互转
支持所有Python标准数据类型和主流第三方库类型,包括numpy、pandas、torch、tensorflow等
提供任意类型之间的双向转换能力,无需手动处理中间格式
提供明确的错误提示和类型检查,确保转换过程的安全性
内置缓存机制,避免重复转换,提升性能
保持与Python内置函数的兼容性,可直接替换使用
自动处理不同库之间的转换细节,如GPU张量的设备管理
pip install e2e-type-converter
from everything2everything import e2e_list, e2e_str, e2e_int
# 基本类型转换
e2e_list(123) # 输出: [123]
e2e_str(None) # 输出: ""
e2e_int("123") # 输出: 123
from everything2everything import TypeConverter
import numpy as np
import torch
# numpy 转换为 torch
arr = np.array([1, 2, 3])
tensor = TypeConverter.numpy_to_torch(arr)
print(type(tensor)) # 输出: <class 'torch.Tensor'>
# torch 转换为 numpy
tensor = torch.tensor([4, 5, 6])
arr = TypeConverter.torch_to_numpy(tensor)
print(type(arr)) # 输出: <class 'numpy.ndarray'>
from everything2everything import TypeConverter
import numpy as np
# 使用通用转换方法
arr = np.array([1, 2, 3])
result = TypeConverter.convert(arr, 'torch') # 转换为torch张量
print(type(result)) # 输出: <class 'torch.Tensor'>
如果您喜欢这个项目,欢迎给我们点个Star,或者参与贡献代码!
EveryThing to EveryThing 类型转换兼容层