Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78254 > unrolled thread

Re: Best way to deal with different data types in a list comprehension

Started byRock Neurotiko <miguelglafuente@gmail.com>
First post2014-09-24 00:05 +0200
Last post2014-09-24 00:05 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Best way to deal with different data types in a list comprehension Rock Neurotiko <miguelglafuente@gmail.com> - 2014-09-24 00:05 +0200

#78254 — Re: Best way to deal with different data types in a list comprehension

FromRock Neurotiko <miguelglafuente@gmail.com>
Date2014-09-24 00:05 +0200
SubjectRe: Best way to deal with different data types in a list comprehension
Message-ID<mailman.14291.1411509984.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Maybe there are a different way, but you can do this:

' '.join([self.get_abbrev()] +
           [str(f['value').encode('utf-8') if type(f['value']) is str else
str(f['value']
            for f in self.filters
            if f.has_key('value')]).strip()

2014-09-24 0:01 GMT+02:00 Larry Martell <larry.martell@gmail.com>:

> I have some code that I inherited:
>
> ' '.join([self.get_abbrev()] +
>            [str(f['value')
>             for f in self.filters
>             if f.has_key('value')]).strip()
>
>
> This broke today when it encountered some non-ascii data.
>
> I changed the str(f['value']) line to f['value'].encode('utf-8'),
> which works fine, except when f['value'] is not a string (it could be
> anything).
>
> Without rewriting this without the list comprehension, how can I write
> this to deal with both strings and non-strings?
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Miguel García Lafuente - Rock Neurotiko

Do it, the devil is in the details.
The quieter you are, the more you are able to hear.
Happy Coding. Code with Passion, Decode with Patience.
If we make consistent effort, based on proper education, we can change the
world.

El contenido de este e-mail es privado, no se permite la revelacion del
contenido de este e-mail a gente ajena a él.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web