我正在尝试从 list 中的 list 获取数据
data1 = [['Once per day', '50 times per day', 'Once per week', 'Twice per day'], ['Serverless', 'Infrastructure as a Service', 'Hybrid Compute', 'Virtual Machine Scale Set']]
data2 = [['Twice per day', '50 times per day', 'Once per day', 'Once per week'], ['Virtual Machine Scale Set', 'Infrastructure as a Service', 'Hybrid Compute', 'Serverless']]
例如,我正在尝试检查 data1 中的第一项在哪个索引中
Sample Output
3
4
作为“每天一次”,data1 中的第一项更改为 data2 中的索引 3,并将 1 添加到索引
回答1
我会遍历 data1[0] 或 data2[0] 的长度并比较每个元素的 value。如果它们不同,那么您就找到了索引。
data1 = [['Once per day', '50 times per day', 'Once per week', 'Twice per day'], ['Serverless', 'Infrastructure as a Service', 'Hybrid Compute', 'Virtual Machine Scale Set']]
data2 = [['Twice per day', '50 times per day', 'Once per day', 'Once per week'], ['Virtual Machine Scale Set', 'Infrastructure as a Service', 'Hybrid Compute', 'Serverless']]
d1 = data1[0]
d2 = data2[0]
for i in range(len(d1)):
if d1[i] != d2[i]
print(0,i)
break
回答2
访问 list 中的 list 是通过连续的方括号完成的,例如 print data1[0][0]
将返回 Once per day
。其余过程将通过 for 循环完成!
回答3
一个简短的方法来做到这一点:
L = [(data2[i].index(data1[i][0] ) +1) for i in range(len(data1)) ]
print(L)
输出 :
[3, 4]