如何迭代python中的多维任意列表

问题描述

我有一个表达式树,它生成树列表。列表因用于计算数字的表达式而异,例如,

T = ['-',['+',['-',75,10,3]],100,50]],3]

T = ['+',['*',5,4],['/',20,2] ]]

我想遍历 Tree 的每个元素并存储运算符的索引、所有运算符的列表、数字的索引和数组中的数字列表。为此,我试图找出一种方法,我可以循环到列表中,然后只检查类型是否是一个字符串,这意味着它是一个运算符,并通过附加将该索引和值存储在相应的数组中,否则如果类型为数字,然后将其与所有数字一起存储在数组中。

我尝试了下面的代码

T = T =  ['-',3]
for i in range(0,len(T)):
    if type(T[i]) != int:
        for j in range(0,len(T[i])):
            print(T[i][j])
    else:
        print(T[i])

运行代码给出输出

-
+
['-',3]]
['-',50]
3

我们基本上想要实现的是进入列表,直到我们可以根据列表大小。关于如何实现的任何想法?

我的回答基本上是:

address list of the operators is  [[0],[1,0],1,2,0]] 
    
list of the operators is ['-','+','-','-'] 
    
address of the numbers is [[1,1],2],[2]] 
    
list of the numbers is [75,3,50,3] 

解决方法

您可以使用递归函数。可以改进以下内容,例如同时处理运算符和数字。

T = ['-',['+',['-',75,10,3]],100,50]],3]

def evaluate(arg): # not requested,but why not?
    if type(arg) is list:
        return eval(f'{evaluate(arg[1])} {arg[0]} {evaluate(arg[2])}')
    else:
        return arg

def get_ops(arg): # returns (list of operators,list of their indices)
    ops = [arg[0]] # first elem is an operator,so add it
    idx = [[0]] # add this index 0
    for i in (1,2): # for each position 1 and 2,if type(arg[i]) is list: # if NOT scalar
            ops_sub,idx_sub = get_ops(arg[i]) # recurse!
            ops += ops_sub # add the list of ops from the sublist
            idx += [[i] + x for x in idx_sub] # add idx list from the sublist,# while the position of the sublist
                                              # being PREPENDED to each idx
    return ops,idx

def get_nums(arg): # basically the same goes here
    nums = []
    idx = []
    for i in (1,2):
        if type(arg[i]) is list:
            nums_sub,idx_sub = get_nums(arg[i])
            nums += nums_sub
            idx += [[i] + x for x in idx_sub]
        else: # if scalar,this IS a number,so add it to the output
            nums.append(arg[i])
            idx.append([i])
    return nums,idx

print(get_ops(T))
print(get_nums(T))
print(evaluate(T))

结果:

(['-','+','-','-'],[[0],[1,0],1,2,0]])
([75,3,50,3],[[1,1],2],[2]])
115
,

您可以使用带有 collections.defaultdict 的单个递归生成器函数:

from collections import defaultdict
def get_ops(t,p = []):
   if isinstance(t,int):
      yield ('val',t,p)
   else:
      yield ('op',t[0],p+[0])
      yield from get_ops(t[1],p+[1])
      yield from get_ops(t[2],p+[2])

T,d = ['-',defaultdict(list)
for a,b,c in get_ops(T):
    d[f'{a}_index'].append(c)
    d[f'{a}s'].append(b)

print(dict(d))

输出:

{
  'op_index': [[0],0]],'ops': ['-','val_index': [[1,[2]],'vals': [75,3]
}