Arrays/List, filters, Pytho, Ruby
Josh Benner
sjbenner at gmail.com
Fri Feb 11 17:16:49 EST 2011
On Fri, Feb 11, 2011 at 1:51 PM, Dan Stromberg <drsalists at gmail.com> wrote:
> On Fri, Feb 11, 2011 at 1:43 PM, André Roberge <andre.roberge at gmail.com>
> wrote:
> > On Friday, February 11, 2011 5:24:15 PM UTC-4, LL.Snark 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...
> >>
> > You could go with something like (untested)
> > t = [6,7,8,6,7,9,8,4,3,6,7]
> > for i, j in enumerate(t):
> > if j < t[0]:
> > break
> > else:
> > i = 0
> >
> > ;-)
> >
> >
> >
> >> I'm using Python 3.
> >>
> >> Thx
>
> >>> t = [6,7,8,6,7,9,8,4,3,6,7]
> >>> generator = (element for element in t[1:] if element >= t[0])
> >>> print(next(generator))
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Since we only care about the 1st generated value ...
t = [6,7,8,6,7,9,8,4,3,6,7]
t.index(next((elem for elem in t[1:] if elem < t[0])))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110211/cb5ec6a3/attachment.html>
More information about the Python-list
mailing list