有没有更好的方法来遍历多维元组/列表?

Is there a better way to traverse a multidimensional tuple / list?

我得到了这样的数据样本:

   [1.212,3.54,[4.123],[5.5343],[[2,3.2],[9.345,4.102]]]
   ((1.41231,3.312),4.212,6.312)
   ["hello",1.232,"3.555"]

我的最终目的是序列化这些数据,但是这些列表中的一些数据不是 python 类型,比如 python float--sympy.Core.Float ,所以我必须阅读那些多维的数组,找出那些 Sympy.core.Float 类型的数字,然后像这样进行类型转换:“float(number)”,那么有没有简单的方法来完成这个?

这是零件代码:

def RecursData(datas,final_list):
    for index ,value in enumerate(datas):
        if(isinstance(value,(tuple,list))):
            tmp_data_list  = list(value)
            RecursData(tmp_data_list,final_list)
        elif isinstance(value,(float,Float)):
            final_list.append(float(value))
        else:
            final_list.append(value)

在你的情况下我会这样做:


your_data = [1.212,3.54,[4.123],[5.5343],[[2,3.2],[9.345,4.102]]]

def itter(lst):
    end_goal = []
    for x in lst:
        if hasattr(x, '__iter__'):
            end_goal.append(itter(x))
        else:
          # here do whatever you need to do
          # so if you have to reconvert data

          if isinstance(x,Float):
              x = float(x)

          end_goal.append(x)


    return end_goal


# then just run the function/
print(itter(your_data))

这会将所有 Float 值替换为 python floats