0

I am trying to perform different action on one item in every iteration. Help me out with the below code in python

router1 = "10.11.12.13"
router2 = "10.11.12.14"


tunnel1, tunnel2, tunnel3, tunnel4 = "tunnel01","tunnel02","tunnel03","tunnel04"

router = [router1, router2]
tunnel = [tunnel1, tunnel2, tunnel3, tunnel4]

for each in router:
    for eachtunnel in tunnel:


#Here in the first iteration I have to bring up tunnel1 on router1 and shut remaining tunnels on both router 1 and 2.

#In the second iteration I have to bring up tunnel2 on router1 and shut remaining tunnels on both the routers.

It should go till 8 iterations where only one tunnel is up and remaining are down.

In this case I took the number of tunnels per router as 4 but it may vary. Kindly suggest how can I achieve this.

Rakesh
  • 81,458
  • 17
  • 76
  • 113

2 Answers2

0

How about this:

routers = [router1, router2]
tunnels = [tunnel1, tunnel2, tunnel3, tunnel4]

# Assuming everything is down at the beginning

for router in routers:
    for tunnel in tunnels:
        up(router, tunnel)
        # do stuff
        down(router, tunnel)

I've changed the variable names a bit for readability.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • No I cannot do this, It should be like in the first iteration only first tunnel of router 1 should be up and remaining all tunnels should be down. In this case all the 4 tunnels of router2 are down. @AlexHall – Surender Reddy Apr 23 '19 at 09:33
  • @SurenderReddy but that's not a contradiction, if the remaining tunnels should be down, that includes the tunnels of router2. – Alex Hall Apr 23 '19 at 10:04
0

This should work. I would loop on both tunnel and router twice, and if the inner loop's router and tunnel matches the outer loop's router and tunnel, we make it up, else we make it down

def up(tunnel, router):
    print('up', tunnel, router)

def down(tunnel, router):
    print('down', tunnel, router)

for r1 in router:
    for t1 in tunnel:
        for r2 in router:
            for t2 in tunnel:
                if r1 == r2 and t1 == t2:
                    up(r1, t1)
                else:
                    down(r2, t2)

Sample output is

up 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
up 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
up 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
up 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
up 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
up 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
up 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
up 10.11.12.14 tunnel04

A bad list-comprehension approach will be.

[up(r1, t1) if r1 == r2 and t1 == t2 else down(r2, t2) for t1 in tunnel for r1 in router for r2 in router for t2 in tunnel]

You can also use itertools to create a cartesian product itertools.product and then iterate through them.

from itertools import product, tee
#Create cartesian production of router and tunnel, and convert to list
prod = product(router, tunnel)
#Convert iterator to list
li = list(prod)

#Loop through both lists and perform up/down accordingly
for r1, t1 in li:
    for r2, t2 in li:
        if r1 == r2 and t1 == t2:
            up(r1, t1)
        else:
            down(r2, t2)
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • Aren't `li1` and `li2` the same? Why the `tee`? – Alex Hall Apr 23 '19 at 10:04
  • Hi Devesh, Thank you for the help. I do have another query in addition to this, while we have tunnels 1,2,3,4 up in router1 I have to shutdown core1, core 2 and bring up core3 and core4. In the same way I have to shut core3,4 and take up core1, core2 while the tunnels 1,2,3,4 are up in router 2. output is expected as below: – Surender Reddy Apr 24 '19 at 05:39
  • Update your question instead of commenting here please! – Devesh Kumar Singh Apr 24 '19 at 05:40
  • down core1,core2 up core3,core4 up core1,core2 down core3,core4 – Surender Reddy Apr 24 '19 at 05:43
  • Sure Devesh, I already posted my query. You can find it here: https://stackoverflow.com/questions/55823021/i-want-to-know-if-we-can-perform-two-different-actions-using-for-loop-for-list?noredirect=1#comment98311538_55823021 – Surender Reddy Apr 24 '19 at 05:47
  • Cool i posted my answer there please check – Devesh Kumar Singh Apr 24 '19 at 06:27