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


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

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

Started byChris Kaynor <ckaynor@zindagigames.com>
First post2014-09-23 15:17 -0700
Last post2014-09-23 15:17 -0700
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 Chris Kaynor <ckaynor@zindagigames.com> - 2014-09-23 15:17 -0700

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

FromChris Kaynor <ckaynor@zindagigames.com>
Date2014-09-23 15:17 -0700
SubjectRe: Best way to deal with different data types in a list comprehension
Message-ID<mailman.14292.1411510664.18130.python-list@python.org>

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

On Tue, Sep 23, 2014 at 3:01 PM, Larry Martell <larry.martell@gmail.com>
wrote:

> 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.
>

One option would be to do the processing in unicode, and convert to utf-8
only when needed:

u' '.join([self.get_abbrev()] +
           [unicode(f['value')
            for f in self.filters
            if f.has_key('value')]).strip()

If needed, add a .encode('utf-8') to the end.


>
> 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
>

[toc] | [standalone]


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


csiph-web