英文:
Python converting strings and ints to an unknown, but given data type
问题
我有一个作业,我得到了一个包含字符串数字、整数或浮点数的元组列表,例如,我得到了一个包含(ID,Class,Grade,Height)
的元组:
A = [('123121','24',23,12.0),(23667,64,78,15.5)]
我还得到了一个包含我想要转换的内容以及要将其转换为的类型的元组列表。例如,我得到了这个列表:
types=[('ID','int'),('grade','S15'),('height','float'),('class','int')]
现在,我不知道类型元组中的每个值将是什么,它可以是整数、浮点数、dtype或其他任何类型。我搜索到了这段代码:
from typing import TypeVar, Type
T = TypeVar('T')
def cast(input, cast_type: Type[T]):
casted_value = cast_type(input)
return casted_value
它似乎对整数、浮点数、字符串都有效,但对于S15
,即NumPy dtype,它不起作用,我会得到错误:
casted_value = cast_type(input)
TypeError: Preliminary-API: Flexible/Parametric legacy DType '<class 'numpy.dtype[bytes_]'>' can only be instantiated using `np.dtype(...)`
在我的示例中,转换后打印A后的期望输出应该是:
A = [(123121,b'24',23.0,12),(23667,b'64',78.0,15)]
如何将某些东西转换为给定但在给定之前未知的数据类型?
英文:
I have an assignment, I'm given a list of tuples, that contain string of numbers, ints or float for example, I get a tuple that contains (ID,Class,Grade,Height)
:
A = [('123121','24',23,12.0),(23667,64,78,15.5)]
I am also given a list of tuples, so that each tuple contains the thing I want to convert, and what to convert it to.
for example I get this list:
types=[('ID','int'),(grade,'S15'),('height','float'),('class','int')]
now, I do not know what each value in the type tuple will be, it could be int, float, dtype, whatever.
I searched and I found this piece of code:
from typing import TypeVar, Type
T = TypeVar('T')
def cast(input, cast_type: Type[T]):
casted_value = cast_type(input)
return casted_value
it seems to work fine for int, float, string, but for S15
which is a numpty dtype, it doesn't work and I get the error:
casted_value = cast_type(input)
TypeError: Preliminary-API: Flexible/Parametric legacy DType '<class 'numpy.dtype[bytes_]'>' can only be instantiated using `np.dtype(...)`
in my example the wanted output after printing A after conversions should be:
A = [(123121,b'24',23.0,12),(23667,b'64',78.0,15)]
how can I convert something to a given but unknown until given data type?
答案1
得分: 1
我不确定您想要什么,但以下是如何使用此元组列表和数据类型规范创建一个结构化数组
的方法。
A = [('123121', '24', 23, 12.0), (23667, 64, 78, 15.5)]
types = [('ID', 'int'), ('grade', 'S15'), ('height', 'float'), ('class', 'int')]
arr = np.array(A, dtype=types)
它的完整repr
显示如下:
arr
array([(123121, b'24', 23., 12), ( 23667, b'64', 78., 15)],
dtype=[('ID', '<i4'), ('grade', 'S15'), ('height', '<f8'), ('class', '<i4')])
更紧凑的str
表示如下:
print(arr)
[(123121, b'24', 23., 12) ( 23667, b'64', 78., 15)]
这是一个形状为(2,)的数组,具有4个字段。arr[0]
获取一个记录。arr['height']
获取一个字段。
英文:
I'm not sure what you want, but here's how to make a structured array
with this list of tuples and dtype specification.
In [90]: A = [('123121','24',23,12.0),(23667,64,78,15.5)]
In [92]: types=[('ID','int'),('grade','S15'),('height','float'),('class','int')]
With matching list of tuples, the basic array
function produces:
In [93]: arr = np.array(A, dtype=types)
It's full repr
display is:
In [94]: arr
Out[94]:
array([(123121, b'24', 23., 12), ( 23667, b'64', 78., 15)],
dtype=[('ID', '<i4'), ('grade', 'S15'), ('height', '<f8'), ('class', '<i4')])
and the more compact str
:
In [95]: print(arr)
[(123121, b'24', 23., 12) ( 23667, b'64', 78., 15)]
This is (2,) shape array, with 4 fields. arr[0]
gets one record. arr['height']
gets one field.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论