Вопрос

I'd like to get all the permutations of a list but without any repetitions, regardless of ordering. It's kind of hard to describe so I'll give an example. I'd really like to know the name of this operation because I use it all the time. Also a simple way to achieve this in python would really help me out. Thanks!

e.g

['foo', 'bar', 'la']

==>

['foo', 'bar']
['foo', 'la']
['ba', 'la']
Это было полезно?

Решение

Using itertools.combinations:

>>> import itertools
>>> list(itertools.combinations(['foo', 'bar', 'la'], 2))
[('foo', 'bar'), ('foo', 'la'), ('bar', 'la')]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top