Python列表附加返回值 [英] Python lists append return value

查看:105
本文介绍了Python列表附加返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的二叉树,其后是该图像:

I wanted to create a simple binary tree as followed by this image:

基本上是空的,但是最后一个值是,我创建了根列表:

basically empty , but the last values so I created the root list :

root = [list(),list()]

并创建了一个递归函数来填充所有内容:

and made a recursive function to populate it all :

def TF(nodeT,nodeF , i):
    if i == 35 : return 'done'

    TF(nodeT.append([]),nodeT.append([]) , i = i + 1) #append T , F in the true node
    TF(nodeF.append([]),nodeT.append([]) , i = i + 1) #append T , F in the false node

我的问题是简单的list.append(something)在python中返回"None",所以一旦函数再次被调用(TF(None,None,1))None.append不存在.

my problem is simple list.append(something) in python return "None" so as soon as the function get called again (TF(None,None,1)) None.append doesnt exists.

我该如何解决?预先感谢.

how do I solve this? thanks in advance.

如果您对如何使其更有效或以其他方式有任何建议(也无需测试我的代码,所以不确定如何操作)

also if you have any suggestion on how to make this more efficient or in another way (never got to test my code so I am not sure on how it will do)

(我的最终目标是拥有一个True False映射和一个参数,因此:"FTFTFFFTFTF"将显示字母"M",等等...)

(my final goal is to have a True False map and an argument so : "FTFTFFFTFTF" will bring up the letter "M" etc ...)

推荐答案

在python中,您可以使用"+"运算符来处理两个列表,而保留原始列表.我想这就是您要根据问题标题执行的操作.因此

In python you can use the "+" operator to contatenate two lists leaving the originals untouched. I guess that's what you want to do according to your question title. Thus

[1, 2] + [3] 

将返回

[1, 2, 3]

,因此您可以以实用方式"更多地使用它. 万一您需要

so you can use it more in a "functional fashion". Just in case you need it

[1, 2].__add__([3])

等效于之前的表达式.

is the equivalent to the expression before.

这篇关于Python列表附加返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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