Re: [Python-ideas] Ternary operators in list comprehensions

2017-10-05 Thread Steven D'Aprano
On Thu, Oct 05, 2017 at 05:40:32PM +0200, Jason H wrote:
> >>> a = [1,2,3]
> >>> [ x for x  in a if x & 1]
> [1, 3]
> >>> [ x for x  in a if x & 1 else 'even']
>   File "", line 1
> [ x for x  in a if x & 1 else 'even']
> ^
> SyntaxError: invalid syntax

[(x if x & 1 else 'even') for x in a]


The if clause in the list comprehension determines which items are 
included, and there is no "else" allowed. You don't want to skip any 
values: the list comp should have the same number of items as "a" 
(namely, 3) the comprehension if clause is inappropriate.

Instead, you want the value in the comprehension to conditionally depend 
on x.

By the way, this list is for suggesting improvements and new 
functionality to the language, not for asking for help. You should 
consider asking your questions on python-list or tutor mailing lists 
instead.


-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Ternary operators in list comprehensions

2017-10-05 Thread Rob Cliffe
Putting it another way, your example doesn't make sense.  How would you 
parenthesise it to make it clearer?


    [ (x for x  in a if x & 1) else 'even']     You have an "else" 
without an "if".


    [ x for x  in (a if x & 1 else 'even')]     Using x before it has 
been defined, at least in this line of code.


    [ (x for x  in a) if x & 1 else 'even'] Ditto

Other variants may be possible.  Whereas Jelle's correct version can be 
written as


    [(x if x & 1 else 'even') for x in a]        [1, 'even', 3]

Rob Cliffe


On 05/10/2017 16:44, Jelle Zijlstra wrote:

[x if x & 1 else 'even' for x in a]

An `if` at the end of the comprehension means a condition on whether 
to include the value.


Also, this question would have been better asked on python-list.

2017-10-05 8:40 GMT-07:00 Jason H >:

>>> a = [1,2,3]
>>> [ x for x  in a if x & 1]
[1, 3]
>>> [ x for x  in a if x & 1 else 'even']
  File "", line 1
    [ x for x  in a if x & 1 else 'even']
                                ^
SyntaxError: invalid syntax

I expected [1, 'even', 3]

I would expect that the if expression would be able to provide
alternative values through else.

The work around blows it out to:
l = []
for x in a:
  if x&1:
    l.append(x)
  else:
    l.append('even')


Unless there is a better way?
___
Python-ideas mailing list
Python-ideas@python.org 
https://mail.python.org/mailman/listinfo/python-ideas

Code of Conduct: http://python.org/psf/codeofconduct/




 
	Virus-free. www.avg.com 
 



<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Ternary operators in list comprehensions

2017-10-05 Thread Paul Moore
>>> a = [1,2,3]
>>> [x if x & 1 else 'even' for x in a]
[1, 'even', 3]

You're mixing the if clause of the list comprehension up with a
ternary expresssion. There's no "else" in the list comprehension if
clause.

Paul

On 5 October 2017 at 16:40, Jason H  wrote:
 a = [1,2,3]
 [ x for x  in a if x & 1]
> [1, 3]
 [ x for x  in a if x & 1 else 'even']
>   File "", line 1
> [ x for x  in a if x & 1 else 'even']
> ^
> SyntaxError: invalid syntax
>
> I expected [1, 'even', 3]
>
> I would expect that the if expression would be able to provide alternative 
> values through else.
>
> The work around blows it out to:
> l = []
> for x in a:
>   if x&1:
> l.append(x)
>   else:
> l.append('even')
>
>
> Unless there is a better way?
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Ternary operators in list comprehensions

2017-10-05 Thread Jelle Zijlstra
[x if x & 1 else 'even' for x in a]

An `if` at the end of the comprehension means a condition on whether to
include the value.

Also, this question would have been better asked on python-list.

2017-10-05 8:40 GMT-07:00 Jason H :

> >>> a = [1,2,3]
> >>> [ x for x  in a if x & 1]
> [1, 3]
> >>> [ x for x  in a if x & 1 else 'even']
>   File "", line 1
> [ x for x  in a if x & 1 else 'even']
> ^
> SyntaxError: invalid syntax
>
> I expected [1, 'even', 3]
>
> I would expect that the if expression would be able to provide alternative
> values through else.
>
> The work around blows it out to:
> l = []
> for x in a:
>   if x&1:
> l.append(x)
>   else:
> l.append('even')
>
>
> Unless there is a better way?
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Ternary operators in list comprehensions

2017-10-05 Thread Jason H
>>> a = [1,2,3]
>>> [ x for x  in a if x & 1]
[1, 3]
>>> [ x for x  in a if x & 1 else 'even']
  File "", line 1
[ x for x  in a if x & 1 else 'even']
^
SyntaxError: invalid syntax

I expected [1, 'even', 3]

I would expect that the if expression would be able to provide alternative 
values through else.

The work around blows it out to:
l = []
for x in a:
  if x&1:  
l.append(x)
  else:
l.append('even')


Unless there is a better way?
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Changes to the existing optimization levels

2017-10-05 Thread Diana Clarke
Thanks, Nick!

I'll let this sink in today and give it a shot tomorrow.

Have a great weekend,

--diana

> * Switch to your suggested "set-of-strings" API at the Python level,
> with the Python level integer interface retained only for backwards
> compatibility
> * Keep the current integer-based *C* optimization API, but redefine
> the way that value is interpreted, rather than passing Python sets
> around
>
> The Python APIs would then convert the Python level sets to the
> bitfield representation almost immediately for internal use, but you
> wouldn't need to mess about with the bitfield yourself when calling
> the Python APIs.
>
> The difference I see relates to the fact that in Python:
>
> * sets of strings are easier to work with than integer bitfields
> * adding a new keyword-only argument to existing APIs is straightforward
>
> While in C:
>
> * integer bitfields are easier to work with than Python sets of Python strings
> * supporting a new argument would mean defining a whole new parallel set of 
> APIs
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/