Skip to content

distinct_permutations with incomparable items #832

@hgustafsson

Description

@hgustafsson

Because distinct_permutations uses sorted and < it is not possible to input incomparable items.

This is unexpected since the documentation claims that distinct_permutations is equivalent to set(permutations(iterable).

Example:

>>> set(itertools.permutations(["+","+",0,1]))

{('+', '+', 0, 1),
 ('+', '+', 1, 0),
 ('+', 0, '+', 1),
 ('+', 0, 1, '+'),
 ('+', 1, '+', 0),
 ('+', 1, 0, '+'),
 (0, '+', '+', 1),
 (0, '+', 1, '+'),
 (0, 1, '+', '+'),
 (1, '+', '+', 0),
 (1, '+', 0, '+'),
 (1, 0, '+', '+')}

>>> more_itertools.distinct_permutations(["+","+",0,1])

... more_itertools/more.py:742, in distinct_permutations(iterable, r)
--> 742 items = sorted(iterable)

TypeError: '<' not supported between instances of 'int' and 'str'

Metadata

Metadata

Assignees

No one assigned

    Labels

    pr-welcomeWe are open to PRs that fix this issue - leave a note if you're working on it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions