【发布时间】:2016-09-21 16:17:37
【问题描述】:
也许标题看起来很奇怪,但我在某些时候被困在寻找如何创建这种情况的 for 循环:
我有一个具有这种格式的列表:
data=[['nature', author1, author2, ...author n]
['sport', author1, author2, ....author n]
....
]
我试过这段代码:
authors=[author1, author2, ...author n]
for i in range(len(authors)):
data = [['nature', function(names[i], 'nature')],
['sport', function(names[i], 'sport')
..]
但不幸的是,我猜它会以这种格式返回结果:
data=[['nature', author1]
['sport', author1]
....
]
【问题讨论】:
-
你能更清楚你想要的输出是什么吗?
-
我想要的输出是第一个代码,但带有一个 for 循环
-
给我几分钟,我会给你一个工作代码的例子。我不明白你的挑战在哪里。也许我的代码会清除它。你用的是什么 Python 版本?我在 3.5 上只是想确保我的代码对你有用。
-
我正在使用 python2.7