将任何多维NumPy数组转换为元组的元组...维度数的不可知性 [英] Convert any multi-dimensional numpy array to tuple of tuples of tuples... agnostic of number of dimensions

查看:35
本文介绍了将任何多维NumPy数组转换为元组的元组...维度数的不可知性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

this answer to Convert numpy ndarray to tuple of tuples in optimize method没有提供任何东西,但tuple(tuple(i) for i in a[:,0,:])表示这不存在,但我正在寻找类似于Numpy的.tolist().totuple()方法,因为您不需要事先知道的维数来生成元组的元组...

出于我的需要,这将仅适用于浮点型或整型数值数组。

推荐答案

您可以使用递归函数,该函数独立于维度数转换为元组:

def totuple(a):
    try:
        return tuple(totuple(i) for i in a)
    except TypeError:
        return a
在此答案中找到: Convert numpy array to tuple

这篇关于将任何多维NumPy数组转换为元组的元组...维度数的不可知性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆