How do I generate permutations of length LEN given a list of N Items?

前端 未结 3 1261
小鲜肉
小鲜肉 2020-12-18 19:55

Note: I\'m working in python on this.

For example, given a list:

list = [\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'i\',\'j\']
相关标签:
3条回答
  • 2020-12-18 20:07

    You should use the permutations function from the itertools module.

    >>> import itertools
    >>> lst = ['a','b','c','d','e','f','g','h','i','j']
    >>> itertools.permutations(lst, 3)
    

    Or, if you really want to get combinations, then use the combinations function.

    0 讨论(0)
  • 2020-12-18 20:20
    itertools.permutations(my_list, 3)
    
    0 讨论(0)
  • 2020-12-18 20:21

    Assuming you're in python 2.6 or newer:

    from itertools import permutations
    for i in permutations(your_list, 3):
        print i
    
    0 讨论(0)
提交回复
热议问题