Devang Devang - 1 year ago 78
Python Question

Python permutations in order

listA = ["A","B","C","D"]


From this, I want the following output only:

["A","B","C"]
["B","C","D"]
["C","D","A"]
["D","A","B"]


I have looked at various questions on permutations here, but I am not able to do achieve this so far. Any help will be appreciated.

Answer Source

another way to do this - brute force,

def permutation(L):
    for i in range(len(L)):
        x = L[i:i+3]
        length = len(x)
        if length != 3:
            x = x + L[:3-length]
        print(x)


L = ["A","B","C","D"]
permutation(L)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download