python - 在 For 循环中打印变量
问题描述
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
返回列表中的元素以及索引 ([0,3])。然后在将当前索引添加到字符串时添加到print
常量字符串将给出所需的结果(使用)。T1
i
format
推荐阅读
- node.js - 如何从 Cloud Storage for Firebase 获取在特定时间创建的文件?
- r - R - XGBoost。尝试运行 xgb.boost 函数时出错
- php - Laravel Algolia 多列搜索
- mysql - 错误代码:1442。无法更新存储函数/触发器中的表“ArgentineWine”,因为它已被语句使用
- r - 通过在闪亮的应用程序中按下另一个 actionButton() 来禁用和启用 actionButton()
- ios - Xcode 抛出错误“读取依赖文件”
- r - 如何使用 ggplot2 在 R 中创建绘图以绘制百分比堆积条形图?
- python - 使用重复相同次数的范围内的数字填充 Pyspark 数据框列
- vba - 程序是否成功结束时在文本框中显示消息
- git - 如何在现有分支之上合并新的 git 提交历史列表?