[Tutor] about tree input
劉權陞
01patrickliu at gmail.com
Mon Apr 23 12:28:55 EDT 2018
I still have no idea about how to write the code. Can you make a
demonstration?
2018-04-23 0:42 GMT+08:00 Zachary Ware <zachary.ware+pytut at gmail.com>:
> Basically what you want to do here is give Tree a local name (d), loop
> through T reassigning d to d[k_from_T], and then d[k] = v after the loop. T
> can be arbitraily long, and Tree can actually be and contain any indexable
> type.
>
> If this pointer isn't enough to send you in the right direction, I'll try
> to actually write an example when I have a real keyboard in front of me :)
>
> --
> Zach
> (Top-posted in HTML from a phone)
>
> On Sun, Apr 22, 2018, 08:54 劉權陞 <01patrickliu at gmail.com> wrote:
>
>> To deposit a nodes in the tree (adding a new node), for example: put 3: {}
>> in the Tree branch at branch tuple (2, 4, 1, 5) .
>> If don't use :
>> def f(T,k,v):
>> m=len(T)
>> if m==0:
>> Tree[k]=v
>> elif m==1:
>> Tree[T[0]][k]=v
>> elif m==2:
>> Tree[T[0]][T[1]][k]=v
>> elif m==3:
>> Tree[T[0]][T[1]][T[2]][k]=v
>> elif m==4:
>> Tree[T[0]][T[1]][T[2]][T[3]][k]=v
>> elif m==5:
>> Tree[T[0]][T[1]][T[2]][T[3]][T[4]][k]=v
>> ...
>>
>> f( (2, 4, 1, 5),3,{})
>>
>> Is there a dynamic approach(the node tuple can be any length)
>> I want to create a function like f(branch_tuple,key,value)
>>
>> Tree = {1: {2: {3: {...}, 4: {...}, ...}, 3: {2: {...}, 4: {...}, ...}, 4:
>> {2: {...}, 3: {...}, ...}, ...}, 2: {1: {...}, 3: {...}, 4: {...}, ...},
>> 3:
>> {...}, 4: {...}, ...}
>>
>> *(... indicates omission)
>> * (Node tuple represent one of the branches)
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
More information about the Tutor
mailing list