Arrays/List, filters, Pytho, Ruby

Paul Rubin no.email at nospam.invalid
Fri Feb 11 17:46:33 EST 2011


"LL.Snark" <ll.snark at gmail.com> writes:

> 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}

    from itertools import dropwhile

    t=[6,7,8,6,7,9,8,4,3,6,7]
    i = dropwhile(lambda k: t[k]>=t[0], t).next()

Note the above can throw an exception if no suitable element is found.

    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]

That traverses the whole list even if the desired element is near the
beginning of the list. I don't kow if the Ruby version does the same.



More information about the Python-list mailing list