I can't figure out why the following script (run using booi) does not
generate output like I expect:
---------------------------------------------
l = ["1","2","3","4","5"]
s as string
mylist = [s for s in l if s == "2"]
print mylist #I would expect mylist = ["2"] here, but it is []
mylist = [s for s in l if s.Contains("2")]
print mylist #Generates an object ref=null exception
--------------------------------------------
In fact, almost every comprehension I try fails. This snippet
produces [0,0,0,0,0]:
l = [1,2,3,4,5]
i as int
mylist = [i for i in l if i < 2]
print mylist
On Sat, Jun 27, 2009 at 12:13 AM, Rodney Snell <snells...@gmail.com> wrote: > In fact, almost every comprehension I try fails. This snippet > produces [0,0,0,0,0]:
> l = [1,2,3,4,5] > i as int > mylist = [i for i in l if i < 2] > print mylist
> What am I missing here? Thanks in advance.
Interesting bug. It works in the interpreter but not with the compiler. With the compiler you declare 'i' this way for it to work: mylist = [i for i as int in l if i < 2]
Thanks Cedric. Putting the 'as' cast inline works for me too (both
booi and booc).
Not sure what the difference is. The thing I don't like about it is
that it clutters the comprehension syntax making it less readable.
But that will do for now - thanks again!
On Jun 26, 9:26 am, Cedric Vivier <cedr...@neonux.com> wrote:
> On Sat, Jun 27, 2009 at 12:13 AM, Rodney Snell <snells...@gmail.com> wrote:
> > In fact, almost every comprehension I try fails. This snippet
> > produces [0,0,0,0,0]:
> > l = [1,2,3,4,5]
> > i as int
> > mylist = [i for i in l if i < 2]
> > print mylist
> > What am I missing here? Thanks in advance.
> Interesting bug.
> It works in the interpreter but not with the compiler.
> With the compiler you declare 'i' this way for it to work:
> mylist = [i for i as int in l if i < 2]
On Fri, Jun 26, 2009 at 4:24 PM, Rodney Snell <snells...@gmail.com> wrote:
> Thanks Cedric. Putting the 'as' cast inline works for me too (both
> booi and booc).
> Not sure what the difference is. The thing I don't like about it is
> that it clutters the comprehension syntax making it less readable.
> But that will do for now - thanks again!
> On Jun 26, 9:26 am, Cedric Vivier <cedr...@neonux.com> wrote:
> > On Sat, Jun 27, 2009 at 12:13 AM, Rodney Snell <snells...@gmail.com>
> wrote:
> > > In fact, almost every comprehension I try fails. This snippet
> > > produces [0,0,0,0,0]:
> > > l = [1,2,3,4,5]
> > > i as int
> > > mylist = [i for i in l if i < 2]
> > > print mylist
> > > What am I missing here? Thanks in advance.
> > Interesting bug.
> > It works in the interpreter but not with the compiler.
> > With the compiler you declare 'i' this way for it to work:
> > mylist = [i for i as int in l if i < 2]