On Wed, Oct 21, 2009 at 3:30 AM, Albert-Jan Roskam wrote:
> Is the if-else (esp. 'else') in a list comprehension specific for Python 3.x?
> Or did I miss something?
List comps have always allowed a condition since they were introduced:
[ for in if ]
This is the form of the original questi
> Is the if-else (esp. 'else') in a list comprehension specific for Python 3.x?
> Or
> did I miss something?
Its not part of list comprehensions per se, you can use it in any *expression*.
It was introduced in Python 2.5 as a response to C's ternary operator:
C:
x = foo?bar:baz
Python
x =
way
when you do criticize them, you're a mile away and you have their shoes!
~~
--- On Tue, 10/20/09, Alan Gauld wrote:
From: Alan Gauld
Subject: Re: [Tutor] "if clause" in list comprehensions.
To: tutor
icize them, you're a mile away and you have their shoes!
~~
--- On Tue, 10/20/09, Alan Gauld wrote:
> From: Alan Gauld
> Subject: Re: [Tutor] "if clause" in list comprehensions.
> To: tutor@python.org
> D
Ooops, hit send by mistake...
"vince spicer" wrote
Lambda can save the day to keep everything on one line, and leave
variable
type the same:
mylist = ['John', 'Canada', 25, 32, 'right']
new_list = [(lambda y: y.upper() if hasattr(y, 'upper') else y)(a) for a
in
mylist ]
>> ['JACK', 'CANA
"vince spicer" wrote
Lambda can save the day to keep everything on one line, and leave
variable
type the same:
mylist = ['John', 'Canada', 25, 32, 'right']
new_list = [(lambda y: y.upper() if hasattr(y, 'upper') else y)(a) for a
in
mylist ]
>> ['JACK', 'CANADA', 25, 32, 'RIGHT']
Vince
"Emile van Sebille" wrote
a = [item.upper() if type(item) == str else item for item in mylist]
should do it I think.
or even
a = [ str(item).upper() for item in mylist ]
That was my first attempt but the OP wanted his integers preserved
as integers whereas this would convert them to
Is there any compelling reason to write:
>
>[item.upper() for item in my_list if isinstance(item, basestring)]
>
>rather than the following?
>
>[item.upper() for item in my_list if hasattr(item, 'upper')]What happens if
>you have an object in your list that has
an 'upper' merthod that, say, reboo
On Mon, Oct 19, 2009 at 1:20 PM, Alan Gauld wrote:
>
> "Sander Sweers" wrote
>
>>> mylist = ['John', 'Canada', 25, 32, 'right']
>>> a = [item.upper() for item in mylist if type(item) == type('good')]
>>
>> Usually it is recommended to use hasattr() instead of type()
>> hasattr(s, 'upper')
>
> N
On or about 2009 Oct 19, at 3:57 PM, Sander Sweers indited:
I missed that the try: did not return anything. I was thinking more of
something like this.
def upperfy(item):
try:
item.upper()
return item
except AttributeError:
return item
Thanks for correcting me!
Depe
On Mon, Oct 19, 2009 at 9:20 PM, Alan Gauld wrote:
>
>
> "Sander Sweers" wrote
>
> mylist = ['John', 'Canada', 25, 32, 'right']
>>> a = [item.upper() for item in mylist if type(item) == type('good')]
>>>
>>
>> Usually it is recommended to use hasattr() instead of type()
>> hasattr(s, 'upper')
On Mon, Oct 19, 2009 at 2:14 PM, vince spicer wrote:
>
>
> On Mon, Oct 19, 2009 at 1:58 PM, Emile van Sebille wrote:
>
>> On 10/19/2009 12:20 PM Alan Gauld said...
>>
>>
>>> "Sander Sweers" wrote
>>>
>>> mylist = ['John', 'Canada', 25, 32, 'right']
> a = [item.upper() for item in mylist if
On Mon, Oct 19, 2009 at 1:58 PM, Emile van Sebille wrote:
> On 10/19/2009 12:20 PM Alan Gauld said...
>
>
>> "Sander Sweers" wrote
>>
>> mylist = ['John', 'Canada', 25, 32, 'right']
a = [item.upper() for item in mylist if type(item) == type('good')]
>>>
>>> Usually it is recommended t
On 10/19/2009 12:20 PM Alan Gauld said...
"Sander Sweers" wrote
mylist = ['John', 'Canada', 25, 32, 'right']
a = [item.upper() for item in mylist if type(item) == type('good')]
Usually it is recommended to use hasattr() instead of type()
hasattr(s, 'upper')
Nope, they do completely di
2009/10/19 Alan Gauld :
>> Usually it is recommended to use hasattr() instead of type()
>> hasattr(s, 'upper')
>
> Nope, they do completely different things
> I think you might be thinking of isinstance() which can be used instead of
> type(). I see you use hasattr as a means of testing for a me
"Sander Sweers" wrote
mylist = ['John', 'Canada', 25, 32, 'right']
a = [item.upper() for item in mylist if type(item) == type('good')]
Usually it is recommended to use hasattr() instead of type()
hasattr(s, 'upper')
Nope, they do completely different things
I think you might be thinkin
On Mon, Oct 19, 2009 at 11:39 AM, Eduardo Vieira wrote:
> Hello,
> The other day I was making a script and decided to use a list
> compreehension and I found out that this code:
> mylist = ['John', 'Canada', 25, 32, 'right']
> a = [item.upper() for item in mylist if type(item) == type('good')]
> r
2009/10/19 Eduardo Vieira :
> mylist = ['John', 'Canada', 25, 32, 'right']
> a = [item.upper() for item in mylist if type(item) == type('good')]
Usually it is recommended to use hasattr() instead of type()
hasattr(s, 'upper')
> returned this: ['JOHN', 'CANADA', 'RIGHT']
> I was expecting this
Hello,
The other day I was making a script and decided to use a list
compreehension and I found out that this code:
mylist = ['John', 'Canada', 25, 32, 'right']
a = [item.upper() for item in mylist if type(item) == type('good')]
returned this: ['JOHN', 'CANADA', 'RIGHT']
I was expecting this: ['JOH
19 matches
Mail list logo