3 回答

TA贡献1906条经验 获得超3个赞
您的代码非常易读,我不会更改它。
通过列表理解,您可以编写如下内容:
for index, row in enumerate([row for row in listSchedule if row[0] == 'Download']): row[3] = myOtherList[index]

TA贡献1963条经验 获得超6个赞
您可以尝试这样做,但复制 otherlist 以免丢失信息:
[row+[myotherlist.pop(0)] if row[0]=='Download' else row for row in listScheduel]
例如:
list = [['Download',1,2,3],[0,1,2,3],['Download',1,2,3],['Download',1,2,3]]
otherlist = [0,1,2,3,4]
l = [ row+[otherlist.pop(0)] if row[0]=='Download' else row for row in list]
输出:
[['Download', 1, 2, 3, 0],
[0, 1, 2, 3],
['Download', 1, 2, 3, 1],
['Download', 1, 2, 3, 2]]

TA贡献1856条经验 获得超17个赞
我们可以使用一个队列,当满足条件时将其值一一弹出。为了避免复制数据,让我们将队列实现为myOtherList使用迭代器的视图(感谢ShadowRanger)。
queue = iter(myOtherList)
for row in listSchedule:
if row[0] == "Download":
row.append(next(iter))
添加回答
举报