Re: Yet another unique() function...



On Feb 28, 2:18 pm, Paul Rubin <http://phr...@xxxxxxxxxxxxxx> wrote:
Unicode fix (untested):

def unique(seq, keepstr=True):
t = type(seq)
if t in (unicode, str):
t = (list, t('').join)[bool(keepstr)]
seen = []
return t(c for c in seq if not (c in seen or seen.append(c)))

This definitely works. I tried to post a message about this a few
hours ago, but I guess it didn't go through. I've already updated the
recipe and comments.

I think these iterator approaches get more readable as one becomes
used to them.

I agree. I understood your code in a matter of seconds.

.



Relevant Pages

  • Re: Yet another unique() function...
    ... Try your unique on an unicode string, ... def unique: ... if t in (unicode, str): ... return t(c for c in seq if not (c in seen or seen.append(c))) ...
    (comp.lang.python)
  • urllib2.unquote() vs unicode
    ... def testEq: ... Tests with unquotecalled with utxt and stxt cast into str ... or unicode are also successful. ... Why does this test fail while others are successful? ...
    (comp.lang.python)
  • Re: How to use list as key of dictionary?
    ... except TypeError: ... def tupleize: ... if isinstance(non_tuple, str): ... in case x is a unicode. ...
    (comp.lang.python)
  • Re: Break up list into groups
    ... def gengroups0: ... for val in seq: ... usec/pass ... `getgroups3' is a method I got from another post in this thread, ...
    (comp.lang.python)
  • Revised PEP 349: Allow str() to return unicode strings
    ... str() rather than adding a new built-in function. ... Allow strto return unicode strings ... write code that works with either string type and would also make ... We need to upgrade existing libraries, written for str instances, ...
    (comp.lang.python)