0
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)
    

Which results in:

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]

Plus the graphs - how would I amend the code so it'd say:

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]

Any help appreciated :)

cr28
  • 13
  • 2
  • @mightyandweakcoder `t` is a variable with values, doing `str(t)` wont return `T10` etc.. it will transform the entire variable to a string. – David Feb 27 '21 at 14:54

1 Answers1

2

You could do the following:

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)

The 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).

David
  • 8,113
  • 2
  • 17
  • 36