← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #226433]: swapping values in a list while looping through it.

 

Question #226433 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/226433

    Status: Open => Answered

RaiMan proposed the following answer:
if you do not really need the permutations permanently, but only inside
a loop, you might do the swap, do your job and simply swap back.

an example

r = [1,2,3,4,5,6,7,8]
r1 = []
for i in range(len(r)-1):
    r1.append((i+1,i))
print r1 # this is the swapping directive

for i in r1:
    (r[i[0]], r[i[1]]) = (r[i[1]], r[i[0]]) #swaps according to rule i
    print r # here goes your processing
    (r[i[1]], r[i[0]]) = (r[i[0]], r[i[1]]) #swaps back according to rule i

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.