-1

I am converting python lists to tcl lists using "to_tcl" function (own code). I I want to iterate through all lists in Python (Pat 1, Pat 2, ... Pat 4) and convert it to tcl lists (Pat1_tcl, Pat2_tcl, ..., Pat4_tcl) using the function however I get an error in inserting the number (count) in the Pat%d{count}_tcl} = to_tcl(Pat%d{count}) line. How do I properly format the %d integer part?

count = 0
while count < 5:
Pat{%dcount}_tcl} = to_tcl(Pat{%dcount})
print(Pat{count}_tcl)
count += 1
LokSas
  • 21
  • 2
  • 1
    If you're asking "how do I dynamically create several variables, all of which differ by a digit in their names?" consider using a dictionary or a list instead. – Kevin Jan 28 '19 at 19:17
  • If you REALLY need to access variables by name you can use [locals()](https://docs.python.org/3.5/library/functions.html#locals) and [globals()](https://docs.python.org/3.5/library/functions.html#globals). But I recomend to tell us what you exactly want to achieve to get a better answers. – szatkus Jan 28 '19 at 19:23

1 Answers1

1

You can create a list of lists, then iterate over it:

python_lists = [Pat1, Pat2, Pat3, Pat4, Pat5]
tcl_lists = []

for python_list in python_lists:
    tcl_list = to_tcl(python_list)
    print(tcl_list)
    tcl_lists.append(tcl_list)

This example also appends all converted tcl lists to a list named tcl_lists; You can use it to access the converted lists later, as in tcl_lists[2]

nosklo
  • 205,639
  • 55
  • 286
  • 290