Possible to have multiple loop variables?

L

laredotornado

I don't know why I thought this would work, but I would like to have 3
variables in my for loop per iteration. Those familiar will know that
this

ms1 = {'managed1':7019:8020,'managed2':7020:8021}
for m, lp, ssl_lp in ms1.items():
managedServer = create(m,'Server')
print 'creating managed server '+m
managedServer.setListenAddress('147.191.71.70')
managedServer.setListenPort(lp)
managedServer.setEnabled(0)
cd('SSL/cgServer')
managedServer.setEnabled(1)
managedServer.setListenPort(ssl_lp)
managedServer.setCluster(clus1)

causes

File "/export/third-party/etsbea/home/etsbea/tests/wlst/
createcluster.py", line 9
ms1 = {'managed1':7019:8020,'managed2':7020:8021}
^
SyntaxError: invalid syntax


How would I set up the ms1 array such that I can use three items per
object?

Thanks, - Dave
 
M

Marc 'BlackJack' Rintsch

I don't know why I thought this would work, but I would like to have 3
variables in my for loop per iteration. Those familiar will know that
this

ms1 = {'managed1':7019:8020,'managed2':7020:8021} for m, lp, ssl_lp in
ms1.items():
managedServer = create(m,'Server')
print 'creating managed server '+m
managedServer.setListenAddress('147.191.71.70')
managedServer.setListenPort(lp)
managedServer.setEnabled(0)
cd('SSL/cgServer')
managedServer.setEnabled(1)
managedServer.setListenPort(ssl_lp)
managedServer.setCluster(clus1)

causes

File "/export/third-party/etsbea/home/etsbea/tests/wlst/
createcluster.py", line 9
ms1 = {'managed1':7019:8020,'managed2':7020:8021}
^
SyntaxError: invalid syntax


How would I set up the ms1 array such that I can use three items per
object?

`ms1` is a dictionary and not an array. You could use a tuple as value:

In [5]: ms1 = {'managed1': (7019, 8020), 'managed2': (7020, 8021)}

In [6]: for a, (b, c) in ms1.iteritems():
...: print a, b, c
...:
managed1 7019 8020
managed2 7020 8021

Ciao,
Marc 'BlackJack' Rintsch
 
A

Alan Franzoni

laredotornado was kind enough to say:
How would I set up the ms1 array such that I can use three items per
object?

Use tuples. But that should depend on which values make up the key for your
mapping.

E.g.
ms1 = {('managed1':7019):8020, ('managed2':7020):8021}
ms2 = {'managed1':(7019:8020), 'managed2':(7020:8021)}

depending on what you want, and then iterate over such structure just like
Marc suggested.

Otherwise, you can create a custom class which returns three-tuples when
iterated, in such case you can just extract m, lp, ssl_lp in the for cycle
just like your example.



--
Alan Franzoni <[email protected]>
-
Remove .xyz from my email in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top