Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread Alan Gauld

"Steven D'Aprano"  wrote


List comps can include *any* comparison:

[x+1 for x in data if (3*x+2)**2 > 100*x or x < -5]


Sure, but the wording suggested (maybe wrongly) that the OP 
was a real beginner and so the concept of an expression 
was likely to be foreign. Sticking with equalty or inequality 
seemed likely to be the most familiar test to him/her.


Trying to explain the differnce between is and == seemed 
like it would be a diversion from the primary objective, 
namely how to filter a list comp.


However I guess this discussion has covered that topic too
now, so once again we have managed to kill several 
sparrows with one round of buckshot... :-)



--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread Steven D'Aprano
On Thu, 4 Mar 2010 05:18:40 am Alan Gauld wrote:
> "Steven D'Aprano"  wrote
>
> > Comparisons with None almost always should be one of:
> >
> > item is None
> > item is not None
>
> Yes, but the reason I changed it (I originally had "is not") is that
> != is a more general test for illustrating the use of 'if' within a
> LC which seemed to be the real issue within the question.

List comps can include *any* comparison:

[x+1 for x in data if (3*x+2)**2 > 100*x or x < -5]





-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread Alan Gauld

"Steven D'Aprano"  wrote


Comparisons with None almost always should be one of:

item is None
item is not None


Yes, but the reason I changed it (I originally had "is not") is that != is
a more general test for illustrating the use of 'if' within a LC which
seemed to be the real issue within the question.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread Steven D'Aprano
On Wed, 3 Mar 2010 07:46:39 pm Alan Gauld wrote:

> mylist = [irtem for item in aList where item != None]

Comparisons with None almost always should be one of:

item is None
item is not None

The reason is that "item is None" is ONLY ever true if the item actually 
is the singleton object None (accept no substitutes!).

On the other hand, "item == None" might be true for some customer items. 
So if you actually *do* want to accept substitutes, you can use ==, but 
that would be an unusual thing to do, and worthy of a comment 
explaining that you did mean == and it isn't a mistake.

Likewise for "item != None" versus "item is not None".



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread C.T. Matsumoto

Dave Angel wrote:

Jojo Mwebaze wrote:

Hi There,

i would like to implement the following in lists

assuming

x = 3
y = 4
z = None

i want to create a dynamic list such that

mylist = [ x , y, z ] ,   if z in not None

if z is None then

mylist = [x,y]

Anyhelp!

cheers

Jojo

  


Are there any constraints on x and y ?  If you want to throw out all 
None values, then it's a ready problem.  You try it, and if it doesn't 
quite work, post the code. We'll try to help.


But if only the third value is special, then there's little point in 
making a comprehension of one value.  Just conditionally append the z 
value to the list containing x and y.


DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Hello,

I wrote a solution which isn't correct.

>>> [i for i in mylist if i]
[3, 4]


The if condition should test like the other examples given to the list.

>>> [i for i in mylist if i != None]
[3, 4]

'if i' would also leave out the integer 0.

T
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread Jojo Mwebaze
Thanks to everyone, nice ideas!
cheers


On Wed, Mar 3, 2010 at 10:02 AM, Christian Witts wrote:

> Jojo Mwebaze wrote:
>
>> Hi There,
>>
>> i would like to implement the following in lists
>>
>> assuming
>>
>> x = 3
>> y = 4
>> z = None
>>
>> i want to create a dynamic list such that
>>
>> mylist = [ x , y, z ] ,   if z in not None
>>
>> if z is None then
>>
>> mylist = [x,y]
>>
>> Anyhelp!
>>
>> cheers
>>
>> Jojo
>>
>>
>>
>>
>>
>> 
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
>  |  for , 
>
> So something like `x**2 for x in [1, 2, 3, 4, 5, None, 9] if x != None`
> would iterate over your input set pumping the current item into the variable
> x, it will check "if x != None" and if that condition evaluates true it will
> perform the function you set out to perform.
>
> The predicate section acts as a filter to your data set ensuring the
> variable you are working with meets certain conditions.  If you wanted to
> for eg. still accept the None but perform a different function on it Python
> does allow it like `x**2 if x else 'Not a number' for x in [1, 2, 3, 4, 5,
> None, 9]`.
>
> Hope that helps.
>
> --
> Kind Regards,
> Christian Witts
>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread Dave Angel

Jojo Mwebaze wrote:

Hi There,

i would like to implement the following in lists

assuming

x = 3
y = 4
z = None

i want to create a dynamic list such that

mylist = [ x , y, z ] ,   if z in not None

if z is None then

mylist = [x,y]

Anyhelp!

cheers

Jojo

  


Are there any constraints on x and y ?  If you want to throw out all 
None values, then it's a ready problem.  You try it, and if it doesn't 
quite work, post the code. We'll try to help.


But if only the third value is special, then there's little point in 
making a comprehension of one value.  Just conditionally append the z 
value to the list containing x and y.


DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread Christian Witts

Jojo Mwebaze wrote:

Hi There,

i would like to implement the following in lists

assuming

x = 3
y = 4
z = None

i want to create a dynamic list such that

mylist = [ x , y, z ] ,   if z in not None

if z is None then

mylist = [x,y]

Anyhelp!

cheers

Jojo







___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
  


 |  for , 

So something like `x**2 for x in [1, 2, 3, 4, 5, None, 9] if x != None` 
would iterate over your input set pumping the current item into the 
variable x, it will check "if x != None" and if that condition evaluates 
true it will perform the function you set out to perform.


The predicate section acts as a filter to your data set ensuring the 
variable you are working with meets certain conditions.  If you wanted 
to for eg. still accept the None but perform a different function on it 
Python does allow it like `x**2 if x else 'Not a number' for x in [1, 2, 
3, 4, 5, None, 9]`.


Hope that helps.

--
Kind Regards,
Christian Witts


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread C.T. Matsumoto

Jojo Mwebaze wrote:

Hi There,

i would like to implement the following in lists

assuming

x = 3
y = 4
z = None

i want to create a dynamic list such that

mylist = [ x , y, z ] ,   if z in not None

if z is None then

mylist = [x,y]

Anyhelp!

cheers

Jojo







___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
  

Yes a list comprehension can have if statements.

You can get the values of x and y pretty simply:

>>> [i for i in mylist if i]
[3, 4]


T
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread Alan Gauld


"Jojo Mwebaze"  wrote


i would like to implement the following in lists

assuming

x = 3
y = 4
z = None

i want to create a dynamic list such that

mylist = [ x , y, z ] ,   if z in not None

if z is None then

mylist = [x,y]



Assuming you actually mean that you don;t want to include 
any item that is None you can use an if clause at the end 
of the comprehension:


mylist = [irtem for item in aList where item != None]

and aList is any list, which could be [x,y,z] in your example.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor