"for" cycle with assigning index

dmitrey dmitrey.kroshko at scipy.org
Sun Aug 16 08:36:56 CEST 2009


Thanks all, especially Dennis for your detailed answer.
left_arr_indexes is list of nonnegative integers, eg [0,0,0,1,1,4]
IndDict is a dict like {0: [1,2], 3: [0,1], 10:[0,2,3]}, so that's why
I don't use python list instead.
The code is taken from OpenOpt framework that I develop. Currently I
have implemented another workaround but the solution you proposed can
be used in other parts of code.
Regards, D.

On Aug 16, 4:23 am, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Sat, 15 Aug 2009 13:22:08 -0700, Dennis Lee Bieber
> <wlfr... at ix.netcom.com> declaimed the following in
> gmane.comp.python.general:
>
>         Okay -- my lack of experience with lambda shows... forgot to guard
> the intermediates...
>
> > > for i in xrange(len(Funcs2)):
> > >      Funcs.append(lambda i=i,*args, **kwargs: (Funcs2[i](*args,
> > > **kwargs)[IndDict[left_arr_indexes[i]]]))
>
> > > I get "list indices must be integers, not dict" (and i is equal to
> > > Python dictionary, that is Funcs2)
>
>         So... with the following assumptions (and ignoring all the other
> respondents <G>)...
>
> 1)      Funcs2 IS a dictionary keyed by sequential integers starting at 0
> 2)      Order in Funcs matters; that is, the first element of the list must
> align with the element of the dictionary having key "0"
> 3)      IndDict and left_arr_indexes DO NOT CHANGE CONTENT after the loop
> runs
> 4)      left_arr_indexes is at least as long as Funcs2
>
> assert (min(Funcs2.keys()) == 0
>                         and
>                 max(Funcs2.keys()) == (len(Funcs2) - 1))        #1
> assert len(left_arr_indexes) >= len(Funcs2)          #4
>
> Funcs = [None] * len(Funcs2)                                            #2a
> for ki, fv in Funcs2.items():   #ki->key as index; fv->function value
>         ritmp = IndDict[left_arr_indexes[ki]]                           #3
>         Funcs[ki] = (lambda ri=ritmp, fn=fv, *args, **kwargs:
>                                         (fn(*args, **kwargs)[ri])                       #2b
>
> ri and fn used to protect the intermediates, ritmp and fv, from changes
>
> 2a is needed as the order of .items() is undefined, so .append() can not
> be used. This presets the result list size permitting direct indexing
> (2b) to fill slots in order.
>
> #1 can still fail itself if a key is NOT an integer (what is the min()
> of 0 and "a")
>
>         I still don't know if IndDict is really a list or a dictionary, nor
> what left_array_indexes contains (by name, it is a list of values to
> index into another list -- but could be a list of keys to access a
> dictionary)
>
>         And I'll reiterate: if you are using dictionaries in which the keys
> are sequential integers starting at 0... Replace them with simple
> lists...
> --
>         Wulfraed         Dennis Lee Bieber               KD6MOG
>         wlfr... at ix.netcom.com      HTTP://wlfraed.home.netcom.com/




More information about the Python-list mailing list