访问列表中的列表并检查第二个列表中更改的项目的索引

access List within List and check index for item changed in 2nd 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 中的第一项在 data2 中的哪个索引 例如

Sample Output
3
4

因为“每天一次”data1 中的第一项已更改为 data2 中的索引 3 并向索引添加 1

访问列表中的列表是通过连续的方括号完成的,例如 print data1[0][0] 会 return Once per day。其余过程将通过 for 循环完成!

我会遍历 data1[0] 或 data2[0] 的长度并比较每个元素的值。如果它们不同,那么您已经找到了您的索引。

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

一个简单的方法:

L = [(data2[i].index(data1[i][0] ) +1) for i in range(len(data1)) ]
print(L)

输出:

[3, 4]