On Sat, Aug 4, 2012 at 12:28 PM, Alonzo Quijote <alonzo.quij...@gmail.com> wrote: > Is there a way to define a function which takes > a list (of lists), > a position specified by a list of integers [i0,i1,...,in], and > a value > and returns the result of setting > list[i0][i1]...[in]=value > > The following function works for positions up to length 3 only. > Is it possible to write a general function that does this? > > def setValueAtPosition(list,pos,value): > if len(pos)==1: > list[pos[0]]=value > elif len(pos)==2: > list[pos[0]][pos[1]]=value > elif len(pos)==3: > list[pos[0]][pos[1]][pos[2]]=value > return list > > For example >>>> aa=[1,2,[3,4]] > >>>> setValueAtPosition(aa,[2,0],5) > [1, 2, [5, 4]] > >>>> aa > [1, 2, [5, 4]] > > _______________________________________________
Seems very odd requirement. Anyway here is one way: --------------- def setValue(NestedList, pos, value): nl = NestedList for p in pos[:-1]: nl = nl[p] nl[pos[-1]] = value return NestedList ------------------------- Asokan Pichai If a language is designed for non-programmers, soon only non-programs get written in it. --- Anonymouse _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor