在 For 循环中打印变量

Print Variables in For Loop

from matplotlib.pyplot import figure
T10,T11,T12,T13 = nx.random_tree(10),nx.random_tree(11),nx.random_tree(12),nx.random_tree(13)
T = [T10,T11,T12,T13]
for t in T: 
    print("The Pruefer Code for -",  pruefer_code(t))
    fig = figure()
    axis = fig.add_subplot(111)
    nx.draw(t, **opts, ax = axis)
    

这导致:

The Pruefer Code for -  [2, 8, 1, 9, 5, 5, 6, 8]
The Pruefer Code for -  [9, 6, 10, 7, 4, 8, 10, 4, 6]
The Pruefer Code for -  [4, 1, 4, 8, 11, 11, 8, 3, 4, 8]
The Pruefer Code for -  [8, 7, 11, 4, 2, 7, 9, 1, 5, 10, 7]

加上图表 - 我将如何修改代码,这样它会说:

The Pruefer Code for - T10 [2, 8, 1, 9, 5, 5, 6, 8]
The Pruefer Code for - T11 [9, 6, 10, 7, 4, 8, 10, 4, 6]
The Pruefer Code for - T12 [4, 1, 4, 8, 11, 11, 8, 3, 4, 8]
The Pruefer Code for - T13 [8, 7, 11, 4, 2, 7, 9, 1, 5, 10, 7]

感谢任何帮助:)

您可以执行以下操作:

from matplotlib.pyplot import figure
T10,T11,T12,T13 = nx.random_tree(10),nx.random_tree(11),nx.random_tree(12),nx.random_tree(13)
T = [T10,T11,T12,T13]
for i, t in enumerate(T): 
    print("The Pruefer Code for - T1{:}".format(i),  pruefer_code(t))
    fig = figure()
    axis = fig.add_subplot(111)
    nx.draw(t, **opts, ax = axis)

enuemerate will return the element in a list as well as an index ([0,3]). Then adding to the print the constant string T1 while you adds the current index i to the string will give the desired result (using format).