[Python-de] Liste uniquify

Stefan Behnel python-de at behnel.de
Fr Nov 11 19:46:07 CET 2011


Mike Müller, 11.11.2011 18:55:
> Am 11.11.2011 17:51, schrieb Martin v. Loewis:
>> Wie wärs mit
>>
>> import collections
>> def unique(seq):
>>    return collections.OrderedDict(map(None, seq, [])).keys()
>
> In Python 2.7 geht das:
>
> >>> list(map(None, 'abc', []))
> [('a', None), ('b', None), ('c', None)]
> >>> help(map)
> Help on built-in function map in module __builtin__:
>
> map(...)
>      map(function, sequence[, sequence, ...]) ->  list
>
>      Return a list of the results of applying the function to the items of
>      the argument sequence(s).  If more than one sequence is given, the
>      function is called with an argument list consisting of the corresponding
>      item of each sequence, substituting None for missing values when not all
>      sequences have the same length.  If the function is None, return a list of
>      the items of the sequence (or a list of tuples if more than one sequence).
>
> Aber nicht in Python 3.2:
> >>> list(map(None, 'abc', []))
> []
> >>> help(map)
> Help on class map in module builtins:
>
> class map(object)
>   |  map(func, *iterables) -->  map object
>   |
>   |  Make an iterator that computes the function using arguments from
>   |  each of the iterables.  Stops when the shortest iterable is exhausted.
>
> map behandelt None als Funktion nicht mehr besonders.

Da hilft die "-3" Option beim Portieren - zumindest um das Problem zu finden:

$ /opt/python2.7/bin/python -3
Python 2.7.1+ (2.7:c821d3d335e8, Apr 22 2011, 18:37:12)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> list(map(None, 'abc', []))
__main__:1: DeprecationWarning: map(None, ...) not supported in 3.x; use 
list(...)
[('a', None), ('b', None), ('c', None)]

Stefan


Mehr Informationen über die Mailingliste python-de