如何解决如何迭代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]
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。