Generating circular shifts / reduced Latin Squares in Python

前端 未结 7 1211
旧时难觅i
旧时难觅i 2020-12-31 19:38

Was just wondering what\'s the most efficient way of generating all the circular shifts of a list in Python. In either direction. For example, given a list [1, 2, 3, 4

相关标签:
7条回答
  • 2020-12-31 20:21

    more_itertools is a third-party library that offers a tool for cyclic permutations:

    import more_itertools as mit
    
    
    mit.circular_shifts(range(1, 5))
    # [(1, 2, 3, 4), (2, 3, 4, 1), (3, 4, 1, 2), (4, 1, 2, 3)]
    

    See also Wikipedia:

    A circular shift is a special kind of cyclic permutation, which in turn is a special kind of permutation.

    0 讨论(0)
提交回复
热议问题