# For looping in Jython?

``````machine = 'd'
number = {01,02,03,04}
for x in machine:
for y in number:
print (x,y)
``````

returns

``````('d', 1)
('d', 2)
('d', 3)
('d', 4)
``````

How would a guy return?

``````('d01')
('d02')
('d03')
('d04')
``````

How to concatenate āstrā and āintā objects?

``````machine = 'd'
number = [01,02,03,04]
for x in machine:
for y in number:
print (str(x)+str(y))
``````
1 Like

The following forces two digits for `y` as in your example output:

``````machine = 'd'
number = [01,02,03,04]
for x in machine:
for y in number:
print x + '%02d' % y
``````

If you want the parentheses and quotation marks in the output, change the last line to:

``````		print "('" + x + "%02d')" % y
``````
``````time = system.tag.read("[default]SCADA/Time/Clean Time Entry")
params = {"TBC":0,
"TGC":0,
"TPC":0,
"RCT":0,
"GHS":0,
"TS":time.value,
"TBLNum":group}
#list of machine tables in db
machine = 'd'
number = {99,98,97,96}
for x in (machine):
for y in number:
group = (str(x) + str(y))
for i in group:
print params
``````

returns

``````{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
``````

Why does this print 12 times?
Why is it always passing d99?

How would one return the following?

``````{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
``````
``````time = system.tag.read("[default]SCADA/Time/Clean Time Entry")
#list of machine tables in db
machine = 'd'
number = {99,98,97,96}
for x in (machine):
for y in number:
group = (str(x) + str(y))
params = {"TBC":0,"TGC":0,"TPC":0,"RCT":0,"GHS":0,"TS":time.value,"TBLNum":group}
for group in params:
print params

{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}

``````

LOL getting closer

``````time = system.tag.read("[default]SCADA/Time/Clean Time Entry")
#list of machine tables in db
machine = 'd'
number = {99,98,97,96}
for x in (machine):
for y in (number):
group = (str(x) + str(y))
params = {"TBC":0,"TGC":0,"TPC":0,"RCT":0,"GHS":0,"TS":time.value,"TBLNum":group}
for group in params:
print params
break

``````

You were close, try this.

``````time = system.tag.read("[default]SCADA/Time/Clean Time Entry").value
machine = 'd'
number = [99,98,97,96]
for x in (machine):
for y in (number):
group = (str(x) + str(y))
params = {"TBC":0,"TGC":0,"TPC":0,"RCT":0,"GHS":0,"TS":time,"TBLNum":group}
print params
``````
1 Like

Or similar, cleaning up the variable names a bit:

``````time = system.tag.read("[default]SCADA/Time/Clean Time Entry")
#list of machine tables in db
machines = ["d"]
machine_numbers = [99, 98, 97, 96]

params = []

for machine in machines:
for number in machine_numbers:
params.append(
{
"TBC": 0,
"TGC": 0,
"TPC": 0,
"RCT": 0,
"GHS": 0,
"TS": time.value,
"TBLNum": "%s%s" % (machine, number),
}
)

for group in params:
print group
``````
1 Like

One important thing to note: `number = {99,98,97,96}` creates `number` as a set, which is an unordered sequence. If the final output order matters, you should use a list (`[]`) or tuple (`()`) instead of the set initializer (`{}`).

The order doesnāt matter in this case. Thanks for this example! It helps connect some dots for meā¦This is like string formatting in C. Python is hella wild.

This script for loops through a named query params arg if there are any future readers. Your mileage may vary.

``````time = system.tag.read("[default]SCADA/Time/Clean Time Entry")
#list of machine tables in db
machines = ["d"]
machine_numbers = [01,02,03,04,05,06,07,08,09,10,11,12,13,14,16,17,19,21,22,23]
nq = 'New Folder/clean'
params = []

for machine in machines:
for number in machine_numbers:
params.append(
{
"TBC": 0,
"TGC": 0,
"TPC": 0,
"RCT": 0,
"GHS": 0,
"TS": time.value,
"TBLNum": "%s%s" % (machine, number),
}
)

for group in params:
print group
system.db.runNamedQuery(nq, group)
system.db.clearNamedQueryCache(nq)
``````
``````{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 15:00:00 CDT 2019}
1
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 15:00:00 CDT 2019}
1
``````
1 Like