Python Forum
penjee . com and non-pythonic code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: penjee . com and non-pythonic code (/thread-2136.html)

Pages: 1 2


RE: penjee . com and non-pythonic code - wavic - Feb-28-2017

(Feb-28-2017, 09:54 PM)Ofnuts Wrote: Warning: using a comprehension for the side effects should only be attempted by trained professionals.
numbers = [12, 37, 5, 42, 8, 3]
even,odd=[],[]
_=[[even,odd][i%2].append(i) for i in numbers]

I am totally lost here.  Big Grin What did you do?  Huh

@buran, thank you for this. I was thinking that list.next() is next element from the list but it is next index. And it's reindexing all when I change the list.


RE: penjee . com and non-pythonic code - zivoni - Feb-28-2017

(Feb-28-2017, 10:30 PM)wavic Wrote: I am totally lost here.  Big Grin What did you do?  Huh

Ofnuts uses n % 2 (0 or 1) to select either odd or even list to append number. Comprehension is used to iterate over numbers while external variables - even and odd lists - are modified. Comprehension's result is unimportant and discarded (list of None's).


RE: penjee . com and non-pythonic code - nilamo - Feb-28-2017

(Feb-28-2017, 09:54 PM)Ofnuts Wrote: Warning: using a comprehension for the side effects should only be attempted by trained professionals.
numbers = [12, 37, 5, 42, 8, 3]
even,odd=[],[]
_=[[even,odd][i%2].append(i) for i in numbers]

Cry Shifty Sick *vomit*


RE: penjee . com and non-pythonic code - ichabod801 - Mar-01-2017

Oh, yeah?

def even_odd(n, even = [], odd = []):
    [even, odd][n % 2].append(n)
list(map(even_odd, [12, 37, 5 42, 8, 3]))
even, odd = even_odd.__defaults__



RE: penjee . com and non-pythonic code - Ofnuts - Mar-01-2017

(Mar-01-2017, 01:58 AM)ichabod801 Wrote: Oh, yeah?

def even_odd(n, even = [], odd = []):
    [even, odd][n % 2].append(n)
list(map(even_odd, [12, 37, 5 42, 8, 3]))
even, odd = even_odd.__defaults__

You can't run it several times, because the data is accumulated in the two lists that are used as default values.


RE: penjee . com and non-pythonic code - ichabod801 - Mar-01-2017

(Mar-01-2017, 08:12 AM)Ofnuts Wrote: You can't run it several times, because the data is accumulated in the two lists that are used as default values.

That wasn't in the requirements. If you want new features you'll have to extend the contract.