python中数据类型的嵌套_如何确定Python中嵌套数据结构的类型?

在Python中,需要获取嵌套数据结构的详细类型信息,而不仅仅是顶级类型。文章讨论了如何通过自定义函数实现类似F#的类型签名功能,包括处理列表、元组、字典和NumPy数组的复杂嵌套结构。作者分享了一个能够显示长度和元素类型的解决方案,特别适合在调试大型数据结构时避免显示值的干扰。
摘要由CSDN通过智能技术生成

为了确保正确转换数据结构,需要Python中嵌套类型的详细信息。type()函数适用于简单类型,但不适用于嵌套类型。在

例如在Python中:> data = ([[1,2,3],[4,5,6],[7,8,9]],["a","b","c"])

> type(data)

只给出第一级的类型。对元组中的数组一无所知。在

我希望能像F所做的那样

^{pr2}$

返回独立于值的签名int [] [] * string []* is a tuple item separator

int [] [] is a two dimensional jagged array of int

string [] is a one dimensional array of string

在Python中可以或者如何做到这一点?在

TLDR

目前我在调试器中使用PyCharm,并在“变量”窗口中单击单个变量的查看选项以查看详细信息。问题是输出包含值和混合的类型,我只需要类型签名。当变量类似于(float[50000][784],int[50000])时,值会阻碍。是的,我现在正在调整变量的大小,但这是一个解决办法,而不是一个解决方案。在

例如(array([[ 0., 0., 0., ..., 0., 0., 0.],

[ 0., 0., 0., ..., 0., 0., 0.],

[ 0., 0., 0., ..., 0., 0., 0.],

...,

[ 0., 0., 0., ..., 0., 0., 0.],

[ 0., 0., 0., ..., 0., 0., 0.],

[ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32),

array([7, 2, 1, ..., 4, 5, 6]))

f331028cb538897f90d68205f63a5e90.png(array([[ 0., 0., 0., ..., 0., 0., 0.],

[ 0., 0., 0., ..., 0., 0., 0.],

[ 0., 0., 0., ..., 0., 0., 0.],

...,

[ 0., 0., 0., ..., 0., 0., 0.],

[ 0., 0., 0., ..., 0., 0., 0.],

[ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32),

array([5, 0, 4, ..., 8, 4, 8], dtype=int64))

编辑:

因为这个问题已经被关注了,有人显然在寻找更多的细节,这里是我的修改版本,它也可以处理numpy ndarray。感谢初始版本的Vlad。在

也因为使用了Run Length Encoding的变体,所以不再使用?对于异构类型。在# Note: Typing for elements of iterable types such as Set, List, or Dict

# use a variation of Run Length Encoding.

def type_spec_iterable(iterable, name):

def iterable_info(iterable):

# With an iterable for it to be comparable

# the identity must contain the name and length

# and for the elements the type, order and count.

length = 0

types_list = []

pervious_identity_type = None

pervious_identity_type_count = 0

first_item_done = False

for e in iterable:

item_type = type_spec(e)

if (item_type != pervious_identity_type):

if not first_item_done:

first_item_done = True

else:

types_list.append((pervious_identity_type, pervious_identity_type_count))

pervious_identity_type = item_type

pervious_identity_type_count = 1

else:

pervious_identity_type_count += 1

length += 1

types_list.append((pervious_identity_type, pervious_identity_type_count))

return (length, types_list)

(length, identity_list) = iterable_info(iterable)

element_types = ""

for (identity_item_type, identity_item_count) in identity_list:

if element_types == "":

pass

else:

element_types += ","

element_types += identity_item_type

if (identity_item_count != length) and (identity_item_count != 1):

element_types += "[" + `identity_item_count` + "]"

result = name + "[" + `length` + "]"

return result

def type_spec_dict(dict, name):

def dict_info(dict):

# With a dict for it to be comparable

# the identity must contain the name and length

# and for the key and value combinations the type, order and count.

length = 0

types_list = []

pervious_identity_type = None

pervious_identity_type_count = 0

first_item_done = False

for (k, v) in dict.iteritems():

key_type = type_spec(k)

value_type = type_spec(v)

item_type = (key_type, value_type)

if (item_type != pervious_identity_type):

if not first_item_done:

first_item_done = True

else:

types_list.append((pervious_identity_type, pervious_identity_type_count))

pervious_identity_type = item_type

pervious_identity_type_count = 1

else:

pervious_identity_type_count += 1

length += 1

types_list.append((pervious_identity_type, pervious_identity_type_count))

return (length, types_list)

(length, identity_list) = dict_info(dict)

element_types = ""

for ((identity_key_type,identity_value_type), identity_item_count) in identity_list:

if element_types == "":

pass

else:

element_types += ","

identity_item_type = "(" + identity_key_type + "," + identity_value_type + ")"

element_types += identity_item_type

if (identity_item_count != length) and (identity_item_count != 1):

element_types += "[" + `identity_item_count` + "]"

result = name + "[" + `length` + "]"

return result

def type_spec_tuple(tuple, name):

return name + ""

def type_spec(obj):

object_type = type(obj)

name = object_type.__name__

if (object_type is int) or (object_type is long) or (object_type is str) or (object_type is bool) or (object_type is float):

result = name

elif object_type is type(None):

result = "(none)"

elif (object_type is list) or (object_type is set):

result = type_spec_iterable(obj, name)

elif (object_type is dict):

result = type_spec_dict(obj, name)

elif (object_type is tuple):

result = type_spec_tuple(obj, name)

else:

if name == 'ndarray':

ndarray = obj

ndarray_shape = "[" + `ndarray.shape`.replace("L","").replace(" ","").replace("(","").replace(")","") + "]"

ndarray_data_type = `ndarray.dtype`.split("'")[1]

result = name + ndarray_shape + ""

else:

result = "Unknown type: " , name

return result

我不认为它已经完成了,但到目前为止,它已经解决了我所需要的一切。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值