在while循环中使用的字符串内插入数字迭代

时间:2019-01-28 19:16:00

标签: python

我正在使用“ to_tcl”功能(自己的代码)将python列表转换为tcl列表。我想使用函数迭代Python(Pat 1,Pat 2,... Pat 4)中的所有列表并将其转换为tcl列表(Pat1_tcl,Pat2_tcl,...,Pat4_tcl),但是在插入Pat%d {count} _tcl} = to_tcl(Pat%d {count})行中的数字(计数)。如何正确格式化%d整数部分?

count = 0
while count < 5:
Pat{%dcount}_tcl} = to_tcl(Pat{%dcount})
print(Pat{count}_tcl)
count += 1

1 个答案:

答案 0 :(得分:1)

您可以创建一个列表列表,然后对其进行遍历:

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)

此示例还将所有转换后的tcl列表追加到名为tcl_lists的列表中;您可以稍后使用它来访问转换后的列表,如tcl_lists[2]