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

2024-09-29 17:23:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: to函数代码列表count数字整数tcl
1条回答
网友
1楼 · 发布于 2024-09-29 17:23:03

您可以创建列表列表,然后对其进行迭代:

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]

相关问题 更多 >

    热门问题