On Feb 11, 9:24 pm, "LL.Snark" <ll.sn...@gmail.com> wrote: > Hi, > > I'm looking for a pythonic way to translate this short Ruby code : > t=[6,7,8,6,7,9,8,4,3,6,7] > i=t.index {|x| x<t.first} > > If you don't know Ruby, the second line means : > What is the index, in array t, of the first element x such that x<t[0]. > > If can write it in python several ways : > t=[6,7,8,6,7,9,8,4,3,6,7] > i=0 > while t[i]>=t[0] : i+=1 > > ... not pythonic I think... > > Or : > t=[6,7,8,6,7,9,8,4,3,6,7] > i=[j for j in range(len(t)) if t[j]<t[0]][0] > > ...too cryptic... > > I'm using Python 3. > > Thx
My take (but using Python 2.x): import operator as op from functools import partial from itertools import islice t = [6,7,8,6,7,9,8,4,3,6,7] def first_conditional(seq, first=op.itemgetter(0), pred=op.gt): f = first(seq) cmpfunc = partial(pred, f) for idx, val in enumerate(islice(seq, 1, None)): if cmpfunc(val): return idx + 1 return -1 # or raise an exception? Off top of head, so needs work, but is fairly generic. Jon. -- http://mail.python.org/mailman/listinfo/python-list