对于我正在处理的应用程序,我需要从嵌套元组创建列表,表示每个分支中包含的数据。
作为参考,元组代表一棵霍夫曼树,一个例子是:
tree = (1.0, (0.5, (0.25, (0.125, 'd'),(0.125, 'c')), (0.25, 'b')), (0.5,'a'))
这是从 Huffman 例程创建的,具有以下概率:
a:0.5, b:0.25, c:0.125, d:0.125
我想列出一个看起来像的列表
[['a'],['b','c','d']]
我试过以下代码:
def makeList(tree):
if len(tree) == 2:
return [tree[0]]
else:
rightlist = []
leftlist = []
right = list(tree[1])
left = list(tree[2])
for i in range(1, len(right)):
rightlist.append(right[i])
for i in range(1, len(left)):
leftlist.append(left[i])
return [rightlist, leftlist]
然而这会返回
[['a'],[(0.25, (0.125, 'd'),(0.125,'c')),(0.25,'b')]
这不是我想要的。
我该如何修改上面的代码以产生我想要的输出?
编辑
我制作了一些给出平衡输入的代码:
('a',0.25), ('b', 0.25), ('c', 0.25), ('d',0.25)
产生我想要的输出:
[['a','b'], ['c','d']]
def makeList(tree):
if len(tree) == 2:
print("I'm in here")
return [tree[1]]
else:
right = tree[1]
left = tree[2]
rightlist = []
leftlist = []
for i in range(0, len(right)):
if type(right[i]) == tuple:
print('right: ' + str(right[i]))
rightlist.append(right[i][1])
for i in range(0, len(left)):
if type(left[i]) == tuple:
print('left: ' + str(left[i]))
leftlist.append(left[i][1])
return [rightlist, leftlist]
但是,它在以下输入上失败(下面的输出):
exampleData = [(0.5, 'a'), (0.5,'b')]
[[],[[]]
exampleData = [(0.5, 'a'), (0.25,'b'), (0.25,'c')]
[[],['b'.'c']]
exampleData = [(0.5,'a'), (0.25,'b'), (0.125,'c'), (0.125,'d')]
[[]],['b',(0.125, 'd')]]
然而,这需要通过的黄金标准测试是为随机树创建这些列表:
probs = np.random.dirichlet([1]*4).tolist()
indices = range(0,4)
exampleData = zip(probs, indices)
huffTree = makeHuffmanTree(exampleData)
groups = makeLists(groups)