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
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)
(my final goal is to have a True False map and an argument so : "FTFTFFFTFTF" will bring up the letter "M" etc ...)
append method in Python is to insert an item at the end of the list, but it doesn't return anything but None . In your code: y = x.
append() is a method which performs an action toward new_lst and doesn't return anything. I think you want to . append() the list after the powers in powers loop (where it is at now) but not return it.
There are four methods to add elements to a List in Python. append() : append the element to the end of the list. insert() : inserts the element before the given index. extend() : extends the list by appending elements from the iterable.
append() adds a list inside of a list. Lists are objects, and when you use . append() to add another list into a list, the new items will be added as a single object (item).
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]
will return
[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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With