Re: Removing None objects from a sequence

2008-12-19 Thread Raymond Hettinger
On Dec 12, 7:51 am, Marco Mariani wrote: > Filip Gruszczyński wrote: > > I am not doing it, because I need it. I can as well use "if not elem > > is None", > > I suggest "if elem is not None", which is not quite the same. They are semantically the same. In theory, Filip's would run slower becaus

Re: Removing None objects from a sequence

2008-12-15 Thread bearophileHUGS
Scott David Daniels: > If you want to keep the original's method, but do it in a more Pythonic > way, I would suggest: > >      def deNone4(alist): >          j = 0 >          for val in alist: >              if val is not None: >                  alist[j] = val >                  j += 1 >        

Re: Removing None objects from a sequence

2008-12-15 Thread J. Cliff Dyer
On Mon, 2008-12-15 at 02:11 +, Lie Ryan wrote: > On Fri, 12 Dec 2008 22:55:20 +, Steven D'Aprano wrote: > > > On Fri, 12 Dec 2008 21:18:36 +, Lie Ryan wrote: > >> Personally, I'd prefer VB's version: > >> foo IsNot bar > >> > >> or in pseudo-python > >> foo isnot bar > >> > >> since

Re: Removing None objects from a sequence

2008-12-15 Thread Scott David Daniels
Steven D'Aprano wrote: On Fri, 12 Dec 2008 19:02:24 -0500, Terry Reedy wrote: ... Tim Chase wrote: If you want to literally remove None objects from a list(or mutable sequence) def deNone(alist): n=len(alist) i=j=0 while i < n: if alist[i] is not None: alist[j] = alist

Re: Removing None objects from a sequence

2008-12-15 Thread Steven D'Aprano
On Mon, 15 Dec 2008 05:39:45 +, Lie Ryan wrote: > I was just expressing the > preference that operators should be composed of a single word, > especially since none of the other operators are multi-words Then you should have said so, instead of introducing red-herrings about tired programmer

Re: Removing None objects from a sequence

2008-12-14 Thread Arnaud Delobelle
Lie Ryan writes: > I was just expressing the preference that operators should be > composed of a single word, especially since none of the other > operators are multi-words (Special cases aren't special enough to > break the rules). The only advantage of using 'is not' over 'isnot' is > that we

Re: Removing None objects from a sequence

2008-12-14 Thread Lie Ryan
On Mon, 15 Dec 2008 03:21:21 +, Steven D'Aprano wrote: > On Mon, 15 Dec 2008 02:11:10 +, Lie Ryan wrote: > >>> So given the normal precedence rules of Python, there is no ambiguity. >>> True, you have to learn the rules, but that's no hardship. >> >> *I* know about the precedence rule, b

Re: Removing None objects from a sequence

2008-12-14 Thread Steven D'Aprano
On Mon, 15 Dec 2008 02:11:10 +, Lie Ryan wrote: >> So given the normal precedence rules of Python, there is no ambiguity. >> True, you have to learn the rules, but that's no hardship. > > *I* know about the precedence rule, but a newbie or a tired programmer > might not. He might want to reve

Re: Removing None objects from a sequence

2008-12-14 Thread Lie Ryan
On Fri, 12 Dec 2008 22:55:20 +, Steven D'Aprano wrote: > On Fri, 12 Dec 2008 21:18:36 +, Lie Ryan wrote: >> Personally, I'd prefer VB's version: >> foo IsNot bar >> >> or in pseudo-python >> foo isnot bar >> >> since that would make it less ambiguous. > > "a is not b" is no more ambiguo

Re: Removing None objects from a sequence

2008-12-14 Thread Stephen Thorne
On 2008-12-12, Filip Gruszczyński wrote: > Hi! > > I would like to iterate over a sequence nad ignore all None objects. > The most obvious way is explicitly checking if element is not None, > but it takes too much space. And I would like to get something faster. > I can use > [ sth for sth in self

Re: Removing None objects from a sequence

2008-12-13 Thread Steven D'Aprano
On Sat, 13 Dec 2008 06:00:09 -0800, bearophileHUGS wrote: > Steven D'Aprano: >> The algorithm is unclear: try explaining what you are doing in English, >> and see how difficult it is. > > That algorithm is a standard one, and quite clear. Any programmer worth > his/her/hir salt has to be able to

Re: Removing None objects from a sequence

2008-12-13 Thread Steven D'Aprano
On Sat, 13 Dec 2008 11:07:53 +, Steven D'Aprano wrote: > Now, sure, most of the work in Tim's version is executed in fast C code > instead of slow Python code. Say what??? I'm sorry, I must have been smoking crack when I wrote that. It does nothing of the sort. Tim's version is pure Python.

Re: Removing None objects from a sequence

2008-12-13 Thread bearophileHUGS
Steven D'Aprano: > The algorithm is unclear: try explaining > what you are doing in English, and see how difficult it is. That algorithm is a standard one, and quite clear. Any programmer worth his/her/hir salt has to be able to understand that. I agree that the idiom with the list comp (algorith

Re: Removing None objects from a sequence

2008-12-13 Thread Filip Gruszczyński
Just to be clear, I decided to use generator by alex23, as it seems simple, short and understandable. Still reading this thread was quite interesting, thanks :-) -- Filip Gruszczyński -- http://mail.python.org/mailman/listinfo/python-list

Re: Removing None objects from a sequence

2008-12-13 Thread Steven D'Aprano
On Fri, 12 Dec 2008 19:02:24 -0500, Terry Reedy wrote: > Tim Chase wrote: >>> If you want to literally remove None objects from a list(or >>> mutable sequence) >>> >>> def deNone(alist): >>>n=len(alist) >>>i=j=0 >>>while i < n: >>> if alist[i] is not None: >>>alist[j]

Re: Removing None objects from a sequence

2008-12-12 Thread Terry Reedy
Tim Chase wrote: If you want to literally remove None objects from a list(or mutable sequence) def deNone(alist): n=len(alist) i=j=0 while i < n: if alist[i] is not None: alist[j] = alist[i] j += 1 i += 1 alist[j:i] = [] blist=[None,1,None,2,None,3,None,

Re: Removing None objects from a sequence

2008-12-12 Thread Erik Max Francis
Filip Gruszczyński wrote: I don't mean memory, but space in code ;-) Your goal should be clarity of code, not saving keystrokes. Writing something that is compact in terms of the amount of code to write does not mean its function is clear or even that it is more efficient to run, for that

Re: Removing None objects from a sequence

2008-12-12 Thread Steven D'Aprano
On Fri, 12 Dec 2008 21:18:36 +, Lie Ryan wrote: > On Fri, 12 Dec 2008 11:50:38 -0500, Steve Holden wrote: > >> Kirk Strauser wrote: >>> At 2008-12-12T15:51:15Z, Marco Mariani writes: >>> Filip Gruszczyński wrote: > I am not doing it, because I need it. I can as well use "if no

Re: Removing None objects from a sequence

2008-12-12 Thread Steven D'Aprano
On Fri, 12 Dec 2008 10:08:23 -0600, Kirk Strauser wrote: > At 2008-12-12T15:51:15Z, Marco Mariani writes: > >> Filip Gruszczyński wrote: >> >>> I am not doing it, because I need it. I can as well use "if not elem >>> is None", > >> I suggest "if elem is not None", which is not quite the same. >

Re: Removing None objects from a sequence

2008-12-12 Thread Tim Chase
If you want to literally remove None objects from a list(or mutable sequence) def deNone(alist): n=len(alist) i=j=0 while i < n: if alist[i] is not None: alist[j] = alist[i] j += 1 i += 1 alist[j:i] = [] blist=[None,1,None,2,None,3,None,None,4,None] deNon

Re: Removing None objects from a sequence

2008-12-12 Thread Terry Reedy
If you want to literally remove None objects from a list(or mutable sequence) def deNone(alist): n=len(alist) i=j=0 while i < n: if alist[i] is not None: alist[j] = alist[i] j += 1 i += 1 alist[j:i] = [] blist=[None,1,None,2,None,3,None,None,4,None] deNone(blist)

Re: Removing None objects from a sequence

2008-12-12 Thread Lie Ryan
On Fri, 12 Dec 2008 11:50:38 -0500, Steve Holden wrote: > Kirk Strauser wrote: >> At 2008-12-12T15:51:15Z, Marco Mariani writes: >> >>> Filip Gruszczyński wrote: >>> I am not doing it, because I need it. I can as well use "if not elem is None", >> >>> I suggest "if elem is not None",

Re: Removing None objects from a sequence

2008-12-12 Thread Arnaud Delobelle
alex23 writes: > Rather than a list comprehension, use a generator expression: > > for item in (x for x in sequence if x is not None): >do_something(x) I much prefer for item in sequence: if x is not None: do_something(x) -- Arnaud -- http://mail.python.org/mailman/listinfo/py

Re: Removing None objects from a sequence

2008-12-12 Thread Steve Holden
Kirk Strauser wrote: > At 2008-12-12T15:51:15Z, Marco Mariani writes: > >> Filip Gruszczyński wrote: >> >>> I am not doing it, because I need it. I can as well use "if not elem >>> is None", > >> I suggest "if elem is not None", which is not quite the same. > > So what's the difference exactly?

Re: Removing None objects from a sequence

2008-12-12 Thread MRAB
Kirk Strauser wrote: At 2008-12-12T15:51:15Z, Marco Mariani writes: Filip Gruszczyński wrote: I am not doing it, because I need it. I can as well use "if not elem is None", I suggest "if elem is not None", which is not quite the same. So what's the difference exactly? "foo is not None"

Re: Removing None objects from a sequence

2008-12-12 Thread Marco Mariani
Kirk Strauser wrote: So what's the difference exactly? "foo is not None" is actually surprising to me, since "not None" is True. "0 is True" is False, but "0 is not None" is True. Why is that? Cause I was tired of course, and got the not precedente not right!! Argh -- http://mail.python.org

Re: Removing None objects from a sequence

2008-12-12 Thread Kirk Strauser
At 2008-12-12T15:51:15Z, Marco Mariani writes: > Filip Gruszczyński wrote: > >> I am not doing it, because I need it. I can as well use "if not elem >> is None", > I suggest "if elem is not None", which is not quite the same. So what's the difference exactly? "foo is not None" is actually surp

Re: Removing None objects from a sequence

2008-12-12 Thread Marc 'BlackJack' Rintsch
On Fri, 12 Dec 2008 16:51:15 +0100, Marco Mariani wrote: > Filip Gruszczyński wrote: > > >> I am not doing it, because I need it. I can as well use "if not elem is >> None", > > I suggest "if elem is not None", which is not quite the same. In which way is it not the same? Has the same behavio

Re: Removing None objects from a sequence

2008-12-12 Thread Marco Mariani
Filip Gruszczyński wrote: I am not doing it, because I need it. I can as well use "if not elem is None", I suggest "if elem is not None", which is not quite the same. If you slip such an error in a post, I suggest to practice some time writing correct code before having one-liner contests w

Re: Removing None objects from a sequence

2008-12-12 Thread Vito De Tullio
Filip Gruszczyński wrote: > I checked itertools, but the only thing that > seemed ok, was ifilter - this requires seperate function though, so > doesn't seem too short. is this too much long? >>> from itertools import ifilter >>> for element in ifilter(lambda x: x is not None, [0,1,2,None,3,Non

Re: Removing None objects from a sequence

2008-12-12 Thread pruebauno
On Dec 12, 8:08 am, "Filip Gruszczyński" wrote: > I am not doing it, because I need it. I can as well use "if not elem > is None", but I just wanted to know, if there is some better way of > doing this. I like to know :-) > > And I can't understand why you are becoming so aggressive because of > t

Re: Removing None objects from a sequence

2008-12-12 Thread Steve Holden
Filip Gruszczyński wrote: > Hi! > > I would like to iterate over a sequence nad ignore all None objects. > The most obvious way is explicitly checking if element is not None, > but it takes too much space. And I would like to get something faster. > I can use > [ sth for sth in self.__sth if not s

Re: Removing None objects from a sequence

2008-12-12 Thread Filip Gruszczyński
I am not doing it, because I need it. I can as well use "if not elem is None", but I just wanted to know, if there is some better way of doing this. I like to know :-) And I can't understand why you are becoming so aggressive because of this. Just because I asked for that, doesn't mean, that I wil

Re: Removing None objects from a sequence

2008-12-12 Thread Steven D'Aprano
On Fri, 12 Dec 2008 10:18:35 +0100, Filip Gruszczyński wrote: > Hi! > > I would like to iterate over a sequence nad ignore all None objects. The > most obvious way is explicitly checking if element is not None, but it > takes too much space. Too much space??? seq = [x for x in seq if x is not N

Re: Removing None objects from a sequence

2008-12-12 Thread Tim Rowe
2008/12/12 Filip Gruszczyński : > I don't mean memory, but space in code ;-) Trying to save printer paper for your listings, then? -- Tim Rowe -- http://mail.python.org/mailman/listinfo/python-list

Re: Removing None objects from a sequence

2008-12-12 Thread Filip Gruszczyński
I don't mean memory, but space in code ;-) I'll try this generator :) -- Filip Gruszczyński -- http://mail.python.org/mailman/listinfo/python-list

Re: Removing None objects from a sequence

2008-12-12 Thread alex23
On Dec 12, 7:18 pm, "Filip Gruszczyński" wrote: > Hi! > > I would like to iterate over a sequence nad ignore all None objects. > The most obvious way is explicitly checking if element is not None, > but it takes too much space. And I would like to get something faster. > I can use > [ sth for sth

Re: Removing None objects from a sequence

2008-12-12 Thread Erik Max Francis
Filip Gruszczyński wrote: I would like to iterate over a sequence nad ignore all None objects. The most obvious way is explicitly checking if element is not None, but it takes too much space. That doesn't make much sense; why would iterating over the sequence take more _space_? -- Erik Max

Removing None objects from a sequence

2008-12-12 Thread Filip Gruszczyński
Hi! I would like to iterate over a sequence nad ignore all None objects. The most obvious way is explicitly checking if element is not None, but it takes too much space. And I would like to get something faster. I can use [ sth for sth in self.__sth if not sth is None ], but I don't know if that's