我正在寻找从嵌套列表列表中创建两个字典:
M = {index of list: list}
N = {index of list: reversed list}
例子:
对于初学者,我有以下列表:
L = [[20, 56], [23, 24], [23, 12], [22, 21], [26, 48], [26, 24]]
所以最终结果是:
M = {0: [56, 20], 1: [24, 23], 2: [12, 23], 3: [21, 22], 4: [48, 26], 5: [24, 26]}
N = {0: [20, 56], 1: [23, 24], 2: [23, 12], 3: [22, 21], 4: [26, 48], 5: [26, 24]}
我已经试过了:
M = {}
N = {}
for index, pair in enumerate(L):
M[index] = pair
N[index] = pair.reverse()
但这样做的结果是:
M = {0: [56, 20], 1: [24, 23], 2: [12, 23], 3: [21, 22], 4: [48, 26], 5: [24, 26]}
N = {0: [56, 20], 1: [24, 23], 2: [12, 23], 3: [21, 22], 4: [48, 26], 5: [24, 26]}
我通过这样做解决了它:
N[index] = pair[::-1]
但有人可以告诉我为什么会这样吗?