Python将字符串和整数转换为未知但给定的数据类型。

huangapple go评论66阅读模式
英文:

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 = [(&#39;123121&#39;,&#39;24&#39;,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=[(&#39;ID&#39;,&#39;int&#39;),(grade,&#39;S15&#39;),(&#39;height&#39;,&#39;float&#39;),(&#39;class&#39;,&#39;int&#39;)]

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(&#39;T&#39;)

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 &#39;&lt;class &#39;numpy.dtype[bytes_]&#39;&gt;&#39; can only be instantiated using `np.dtype(...)`

in my example the wanted output after printing A after conversions should be:

A = [(123121,b&#39;24&#39;,23.0,12),(23667,b&#39;64&#39;,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 = [(&#39;123121&#39;,&#39;24&#39;,23,12.0),(23667,64,78,15.5)]


In [92]: types=[(&#39;ID&#39;,&#39;int&#39;),(&#39;grade&#39;,&#39;S15&#39;),(&#39;height&#39;,&#39;float&#39;),(&#39;class&#39;,&#39;int&#39;)]

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&#39;24&#39;, 23., 12), ( 23667, b&#39;64&#39;, 78., 15)],
      dtype=[(&#39;ID&#39;, &#39;&lt;i4&#39;), (&#39;grade&#39;, &#39;S15&#39;), (&#39;height&#39;, &#39;&lt;f8&#39;), (&#39;class&#39;, &#39;&lt;i4&#39;)])

and the more compact str:

In [95]: print(arr)
[(123121, b&#39;24&#39;, 23., 12) ( 23667, b&#39;64&#39;, 78., 15)]

This is (2,) shape array, with 4 fields. arr[0] gets one record. arr[&#39;height&#39;] gets one field.

huangapple
  • 本文由 发表于 2023年6月2日 05:35:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定