Re: ... remove all 0 values
Thanks a lot for your advices, i decided to use the filter() method to sort out the 0. i can ´t use the sum() function cause i need the list afterwards best, Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values
Paul Rubin wrote: > Daniel Austria writes: >> just one question. How can i remove all 0 values in a list? > > I prefer: > >newlist = list(x for x in oldlist if x != 0) > > to the square bracket list comprehension that a few people have > suggested. This is because in python 2.x, the listcomp "leaks" its > index variable into the surrounding scope, but the generator > expression above doesn't. As you indicated, that's been fixed in Py3, though. So if your code works in Py3.x, you can be somewhat sure that the leak doesn't have side effects. Plus, it's pretty easy to ignore those leaks as long as you use a suitable name for the loop variable. Also note that the performance characteristics may not be identical in both cases, depending on where you run your code. Cython, for example, will write a list comprehension out as a rather straight C loop, but when we implement generator expressions in Cython, it may have to get turned into a generator function instead of a loop, so that you'd get a much larger overhead than for the plain list comprehension (although in the simple case above it would likely get optimised away). CPython shows a similar difference: $ python3.1 -m timeit '[x for x in range(1000) if x]' 1 loops, best of 3: 170 usec per loop $ python3.1 -m timeit -s 'r=[i%2 for i in range(2000)]' \ '[x for x in r if x]' 1000 loops, best of 3: 222 usec per loop $ python3.1 -m timeit 'list(x for x in range(1000) if x)' 1000 loops, best of 3: 227 usec per loop $ python3.1 -m timeit -s 'r=[i%2 for i in range(2000)]' \ 'list(x for x in r if x)' 1000 loops, best of 3: 280 usec per loop Not that I'd consider those numbers worth bothering... Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values
Daniel Austria writes: > just one question. How can i remove all 0 values in a list? I prefer: newlist = list(x for x in oldlist if x != 0) to the square bracket list comprehension that a few people have suggested. This is because in python 2.x, the listcomp "leaks" its index variable into the surrounding scope, but the generator expression above doesn't. Usually not a big deal, but an extra bit of hygiene never(?) hurts. -- http://mail.python.org/mailman/listinfo/python-list
Re: ... remove all 0 values
On Jul 8, 10:44 am, Daniel Austria wrote: > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. list.remove() will > only remove the first occurence. Doing that while no exception is > raised is also uncool, right? > > Some suggestions? > > Best, > Dan If you are doing something like this: L = [0, 0, 0, 1, 1, 1, 0] removeZeros(L) number_of_ones = len(L) you can just use sum() like so: number_of_ones = sum(L) HTH -- http://mail.python.org/mailman/listinfo/python-list
Re: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values
J Kenneth King wrote: I was wondering when someone would mention filter() I was happy to see that too. It's clean, faster than list comprehension and in terms of clarity it's only to be expected that the developer is familiar with, or at least willing to look up, the available built-in methods. Regards, Friðrik Már -- http://mail.python.org/mailman/listinfo/python-list
Re: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values
Friðrik Már Jónsson writes: > ma wrote: >> filter(lambda x: x, your_list) > > Good call! Equivalent but more efficient: > > filter(None, your_list) > > Regards, > Friðrik Már I was wondering when someone would mention filter() -- http://mail.python.org/mailman/listinfo/python-list
Re: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values
Daniel Austria wrote: > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. Why not? If you need to potentially look at *all* elements of a list, nothing but a loop will take you there. OTOH, your proposed "remove until nothing is found"-thingy will become quadratic in behavior, as remove also loops over the list - so if you have list with say 10 ones followed by 10 zeros, you will loop ten times for 11 elements, which is in the order of (n/2)**2. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values
ma wrote: filter(lambda x: x, your_list) Good call! Equivalent but more efficient: filter(None, your_list) Regards, Friðrik Már -- http://mail.python.org/mailman/listinfo/python-list
Re: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values
On Wed, 8 Jul 2009 10:54:09 -0400 ma wrote: > filter(lambda x: x, your_list) Or... [x for x in your_list if x] I'm not sure which one is more efficient but I like the syntax of the latter. A smart person could probably figure it out even without knowing Python syntax. Clarity is trump. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
Re: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values
On Jul 8, 2009, at 10:54 AM, ma wrote: filter(lambda x: x, your_list) On Wed, Jul 8, 2009 at 10:44 AM, Daniel Austria wrote: Hi python - hackers, just one question. How can i remove all 0 values in a list? Sure - i can loop over it, but that s not a neat style. list.remove() will only remove the first occurence. Doing that while no exception is raised is also uncool, right? Some suggestions? L = [0, 0, 0, 1, 1, 1, 0] M = [x for x in L if x !=0] Charles Yeomans-- http://mail.python.org/mailman/listinfo/python-list
Re: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values
Daniel Austria a écrit : Hi python - hackers, just one question. How can i remove all 0 values in a list? Sure - i can loop over it, but that s not a neat style. list.remove() will only remove the first occurence. Doing that while no exception is raised is also uncool, right? Some suggestions? the_list = [0, 0, 0, 1, 1, 1, 0] # Simple solution print [x for x in the_list if x != 0] # if you want to mutate the list 'in place': the_list[:] = [x for x in the_list if x != 0] print the_list HTH -- http://mail.python.org/mailman/listinfo/python-list
Re: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values
filter(lambda x: x, your_list) On Wed, Jul 8, 2009 at 10:44 AM, Daniel Austria wrote: > Hi python - hackers, > > just one question. How can i remove all 0 values in a list? Sure - i > can loop over it, but that s not a neat style. list.remove() will > only remove the first occurence. Doing that while no exception is > raised is also uncool, right? > > Some suggestions? > > > Best, > Dan > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
[0, 0, 0, 1, 1, 1, 0] ... remove all 0 values
Hi python - hackers, just one question. How can i remove all 0 values in a list? Sure - i can loop over it, but that s not a neat style. list.remove() will only remove the first occurence. Doing that while no exception is raised is also uncool, right? Some suggestions? Best, Dan -- http://mail.python.org/mailman/listinfo/python-list