让我们说我总共有四个列表,看起来像这样
OrderNum1 = ['110','375']
originaltime = ['12AM','5AM']
OrderNum2 = ['110','215']
newtime = ['2AM', '3AM']
是否只有在ordernum1和ordernum2中的值匹配时才替换原始时间值?
在这种情况下,目标是将12AM替换为2AM,因为ordernum1和ordernum2的索引0均为'110',因此输出看起来像
print(originaltime)
2AM, 5AM
答案 0 :(得分:2)
您可以结合使用zip()
和enumerate()
:
OrderNum1 = ['110','375']
originaltime = ['12AM','5AM']
OrderNum2 = ['110','215']
newtime = ['2AM', '3AM']
for i,(a,b) in enumerate(zip(OrderNum1,OrderNum2)):
if a == b:
originaltime[i] = newtime[i]
答案 1 :(得分:2)
这是您要找的吗?
OrderNum1 = ['110','375']
originaltime = ['12AM','5AM']
OrderNum2 = ['110','215']
newtime = ['2AM', '3AM']
for i in range(len(OrderNum1)):
if OrderNum1[i] == OrderNum2[i]:
originaltime[i] = newtime[i]
print(originaltime)
输出:
['2AM', '5AM']