gpt4 book ai didi

python - 从二维字典数组中提取特定的键/值

转载 作者:太空宇宙 更新时间:2023-11-03 20:05:09 26 4
gpt4 key购买 nike

我有一个二维字典数组,我想从中提取满足特定条件的第一个键的第一个值并将它们放入另一个二维数组中。这是矩阵“coord_curva_testo”(部分):

[[{1: 0}, {2: 1}, {3: 2}, {3: 3}, {4: 4}, {5: 5}, {6: 6}, {6: 7}, {6: 8}, {7: 9}, {8: 10}, {8: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {8: 16}, {8: 17}, {8: 18}, {8: 19}, {8: 20}, {9: 21}, {9: 22}, {9: 23}, {10: 24}, {10: 25}, {11: 26}, {11: 27}, {11: 28}, {11: 29}, {12: 30}, {12: 31}, {12: 32}, {12: 33}, {12: 34}, {13: 35}, {13: 36}, {13: 37}, {14: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}, {15: 54}, {15: 55}, {15: 56}, {15: 57}, {15: 58}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {6: 5}, {6: 6}, {6: 7}, {7: 8}, {8: 9}, {8: 10}, {8: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {8: 16}, {8: 17}, {8: 18}, {8: 19}, {9: 20}, {9: 21}, {9: 22}, {10: 23}, {10: 24}, {11: 25}, {11: 26}, {11: 27}, {11: 28}, {12: 29}, {12: 30}, {12: 31}, {12: 32}, {12: 33}, {13: 34}, {13: 35}, {13: 36}, {14: 37}, {15: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}, {15: 54}, {15: 55}, {15: 56}, {15: 57}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {5: 5}, {5: 6}, {6: 7}, {7: 8}, {7: 9}, {7: 10}, {7: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {8: 16}, {8: 17}, {8: 18}, {9: 19}, {9: 20}, {9: 21}, {10: 22}, {10: 23}, {11: 24}, {11: 25}, {11: 26}, {11: 27}, {12: 28}, {12: 29}, {12: 30}, {12: 31}, {12: 32}, {13: 33}, {13: 34}, {13: 35}, {14: 36}, {15: 37}, {15: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}, {15: 54}, {15: 55}, {15: 56}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {5: 5}, {6: 6}, {7: 7}, {7: 8}, {7: 9}, {7: 10}, {8: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {8: 16}, {8: 17}, {9: 18}, {9: 19}, {9: 20}, {10: 21}, {10: 22}, {11: 23}, {11: 24}, {11: 25}, {11: 26}, {12: 27}, {12: 28}, {12: 29}, {12: 30}, {12: 31}, {13: 32}, {13: 33}, {13: 34}, {14: 35}, {15: 36}, {15: 37}, {15: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}, {15: 54}, {15: 55}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {6: 5}, {7: 6}, {7: 7}, {7: 8}, {7: 9}, {8: 10}, {8: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {8: 16}, {9: 17}, {9: 18}, {9: 19}, {10: 20}, {10: 21}, {11: 22}, {11: 23}, {11: 24}, {11: 25}, {12: 26}, {12: 27}, {12: 28}, {12: 29}, {12: 30}, {13: 31}, {13: 32}, {13: 33}, {14: 34}, {15: 35}, {15: 36}, {15: 37}, {15: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}, {15: 54}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {6: 5}, {6: 6}, {6: 7}, {6: 8}, {7: 9}, {7: 10}, {7: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {9: 16}, {9: 17}, {9: 18}, {10: 19}, {10: 20}, {11: 21}, {11: 22}, {11: 23}, {11: 24}, {12: 25}, {12: 26}, {12: 27}, {12: 28}, {12: 29}, {13: 30}, {13: 31}, {13: 32}, {14: 33}, {15: 34}, {15: 35}, {15: 36}, {15: 37}, {15: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {5: 5}, {5: 6}, {5: 7}, {6: 8}, {6: 9}, {6: 10}, {7: 11}, {7: 12}, {7: 13}, {7: 14}, {8: 15}, {8: 16}, {8: 17}, {9: 18}, {9: 19}, {10: 20}, {10: 21}, {10: 22}, {10: 23}, {11: 24}, {11: 25}, {11: 26}, {11: 27}, {11: 28}, {12: 29}, {12: 30}, {12: 31}, {13: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}, {14: 48}, {14: 49}, {14: 50}, {14: 51}, {14: 52}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {5: 5}, {5: 6}, {6: 7}, {6: 8}, {6: 9}, {7: 10}, {7: 11}, {7: 12}, {7: 13}, {8: 14}, {8: 15}, {8: 16}, {9: 17}, {9: 18}, {10: 19}, {10: 20}, {10: 21}, {10: 22}, {11: 23}, {11: 24}, {11: 25}, {11: 26}, {11: 27}, {12: 28}, {12: 29}, {12: 30}, {13: 31}, {14: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}, {14: 48}, {14: 49}, {14: 50}, {14: 51}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {4: 4}, {4: 5}, {5: 6}, {6: 7}, {6: 8}, {7: 9}, {7: 10}, {7: 11}, {7: 12}, {8: 13}, {8: 14}, {8: 15}, {9: 16}, {9: 17}, {10: 18}, {10: 19}, {10: 20}, {10: 21}, {11: 22}, {11: 23}, {11: 24}, {11: 25}, {11: 26}, {12: 27}, {12: 28}, {12: 29}, {13: 30}, {14: 31}, {14: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}, {14: 48}, {14: 49}, {14: 50}], [{1: 0}, {2: 1}, {3: 2}, {3: 3}, {4: 4}, {5: 5}, {6: 6}, {6: 7}, {7: 8}, {7: 9}, {7: 10}, {7: 11}, {8: 12}, {8: 13}, {8: 14}, {9: 15}, {9: 16}, {10: 17}, {10: 18}, {10: 19}, {10: 20}, {11: 21}, {11: 22}, {11: 23}, {11: 24}, {11: 25}, {12: 26}, {12: 27}, {12: 28}, {13: 29}, {14: 30}, {14: 31}, {14: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}, {14: 48}, {14: 49}], [{1: 0}, {2: 1}, {2: 2}, {3: 3}, {4: 4}, {5: 5}, {5: 6}, {6: 7}, {6: 8}, {6: 9}, {6: 10}, {7: 11}, {7: 12}, {7: 13}, {8: 14}, {8: 15}, {9: 16}, {9: 17}, {9: 18}, {9: 19}, {10: 20}, {10: 21}, {10: 22}, {10: 23}, {10: 24}, {11: 25}, {12: 26}, {12: 27}, {13: 28}, {14: 29}, {14: 30}, {14: 31}, {14: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}, {14: 48}], [{1: 0}, {1: 1}, {2: 2}, {3: 3}, {4: 4}, {4: 5}, {5: 6}, {5: 7}, {5: 8}, {5: 9}, {6: 10}, {6: 11}, {6: 12}, {7: 13}, {7: 14}, {8: 15}, {8: 16}, {9: 17}, {9: 18}, {10: 19}, {10: 20}, {10: 21}, {10: 22}, {10: 23}, {11: 24}, {12: 25}, {12: 26}, {13: 27}, {14: 28}, {14: 29}, {14: 30}, {14: 31}, {14: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}]]

我想为每一行提取第一个带有键 8、10 或 12 的字典,并且从每个键中仅提取第一个值。例如,第一行中的对是 (8,10)、(10, 24) 和 (12,30)。

我是 Python 新手,我发现它是最适合我的目的的语言,这是我的代码(很抱歉,如果您会发现它有点困惑):

dict_rep = defaultdict(list)
line_plot= [[]]

move = 0

key_num = [8,10,12]

for move in range(len(coord_curva_testo[move])):

for d in coord_curva_testo[move]:

for key, value in d.items():

if key in key_num:

dict_rep[key].append(value +1)

line_plot[move].append({list(dict_rep.items([0][0]:list(dict_rep.values())[0][0]})

line_plot[move].append({list(dict_rep.items()[1][0]:list(dict_rep.values())[1][0]})

line_plot[move].append({list(dict_rep.items())[2][0]:list(dict_rep.values())[2][0]})
curva_plot.append([])

但结果是每一行都有与第一行相同的字典。

[[{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}], [{8: 10}, {10: 24}, {12: 30}]

任何人都可以帮助并解释我是否错了,并引导我找到解决问题的好方法吗?

最佳答案

这是您想要的格式吗?

a = [[{1: 0}, {2: 1}, {3: 2}, {3: 3}, {4: 4}, {5: 5}, {6: 6}, {6: 7}, {6: 8}, {7: 9}, {8: 10}, {8: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {8: 16}, {8: 17}, {8: 18}, {8: 19}, {8: 20}, {9: 21}, {9: 22}, {9: 23}, {10: 24}, {10: 25}, {11: 26}, {11: 27}, {11: 28}, {11: 29}, {12: 30}, {12: 31}, {12: 32}, {12: 33}, {12: 34}, {13: 35}, {13: 36}, {13: 37}, {14: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}, {15: 54}, {15: 55}, {15: 56}, {15: 57}, {15: 58}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {6: 5}, {6: 6}, {6: 7}, {7: 8}, {8: 9}, {8: 10}, {8: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {8: 16}, {8: 17}, {8: 18}, {8: 19}, {9: 20}, {9: 21}, {9: 22}, {10: 23}, {10: 24}, {11: 25}, {11: 26}, {11: 27}, {11: 28}, {12: 29}, {12: 30}, {12: 31}, {12: 32}, {12: 33}, {13: 34}, {13: 35}, {13: 36}, {14: 37}, {15: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}, {15: 54}, {15: 55}, {15: 56}, {15: 57}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {5: 5}, {5: 6}, {6: 7}, {7: 8}, {7: 9}, {7: 10}, {7: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {8: 16}, {8: 17}, {8: 18}, {9: 19}, {9: 20}, {9: 21}, {10: 22}, {10: 23}, {11: 24}, {11: 25}, {11: 26}, {11: 27}, {12: 28}, {12: 29}, {12: 30}, {12: 31}, {12: 32}, {13: 33}, {13: 34}, {13: 35}, {14: 36}, {15: 37}, {15: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}, {15: 54}, {15: 55}, {15: 56}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {5: 5}, {6: 6}, {7: 7}, {7: 8}, {7: 9}, {7: 10}, {8: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {8: 16}, {8: 17}, {9: 18}, {9: 19}, {9: 20}, {10: 21}, {10: 22}, {11: 23}, {11: 24}, {11: 25}, {11: 26}, {12: 27}, {12: 28}, {12: 29}, {12: 30}, {12: 31}, {13: 32}, {13: 33}, {13: 34}, {14: 35}, {15: 36}, {15: 37}, {15: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}, {15: 54}, {15: 55}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {6: 5}, {7: 6}, {7: 7}, {7: 8}, {7: 9}, {8: 10}, {8: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {8: 16}, {9: 17}, {9: 18}, {9: 19}, {10: 20}, {10: 21}, {11: 22}, {11: 23}, {11: 24}, {11: 25}, {12: 26}, {12: 27}, {12: 28}, {12: 29}, {12: 30}, {13: 31}, {13: 32}, {13: 33}, {14: 34}, {15: 35}, {15: 36}, {15: 37}, {15: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}, {15: 54}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {6: 5}, {6: 6}, {6: 7}, {6: 8}, {7: 9}, {7: 10}, {7: 11}, {8: 12}, {8: 13}, {8: 14}, {8: 15}, {9: 16}, {9: 17}, {9: 18}, {10: 19}, {10: 20}, {11: 21}, {11: 22}, {11: 23}, {11: 24}, {12: 25}, {12: 26}, {12: 27}, {12: 28}, {12: 29}, {13: 30}, {13: 31}, {13: 32}, {14: 33}, {15: 34}, {15: 35}, {15: 36}, {15: 37}, {15: 38}, {15: 39}, {15: 40}, {15: 41}, {15: 42}, {15: 43}, {15: 44}, {15: 45}, {15: 46}, {15: 47}, {15: 48}, {15: 49}, {15: 50}, {15: 51}, {15: 52}, {15: 53}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {5: 5}, {5: 6}, {5: 7}, {6: 8}, {6: 9}, {6: 10}, {7: 11}, {7: 12}, {7: 13}, {7: 14}, {8: 15}, {8: 16}, {8: 17}, {9: 18}, {9: 19}, {10: 20}, {10: 21}, {10: 22}, {10: 23}, {11: 24}, {11: 25}, {11: 26}, {11: 27}, {11: 28}, {12: 29}, {12: 30}, {12: 31}, {13: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}, {14: 48}, {14: 49}, {14: 50}, {14: 51}, {14: 52}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {5: 4}, {5: 5}, {5: 6}, {6: 7}, {6: 8}, {6: 9}, {7: 10}, {7: 11}, {7: 12}, {7: 13}, {8: 14}, {8: 15}, {8: 16}, {9: 17}, {9: 18}, {10: 19}, {10: 20}, {10: 21}, {10: 22}, {11: 23}, {11: 24}, {11: 25}, {11: 26}, {11: 27}, {12: 28}, {12: 29}, {12: 30}, {13: 31}, {14: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}, {14: 48}, {14: 49}, {14: 50}, {14: 51}], [{1: 0}, {2: 1}, {3: 2}, {4: 3}, {4: 4}, {4: 5}, {5: 6}, {6: 7}, {6: 8}, {7: 9}, {7: 10}, {7: 11}, {7: 12}, {8: 13}, {8: 14}, {8: 15}, {9: 16}, {9: 17}, {10: 18}, {10: 19}, {10: 20}, {10: 21}, {11: 22}, {11: 23}, {11: 24}, {11: 25}, {11: 26}, {12: 27}, {12: 28}, {12: 29}, {13: 30}, {14: 31}, {14: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}, {14: 48}, {14: 49}, {14: 50}], [{1: 0}, {2: 1}, {3: 2}, {3: 3}, {4: 4}, {5: 5}, {6: 6}, {6: 7}, {7: 8}, {7: 9}, {7: 10}, {7: 11}, {8: 12}, {8: 13}, {8: 14}, {9: 15}, {9: 16}, {10: 17}, {10: 18}, {10: 19}, {10: 20}, {11: 21}, {11: 22}, {11: 23}, {11: 24}, {11: 25}, {12: 26}, {12: 27}, {12: 28}, {13: 29}, {14: 30}, {14: 31}, {14: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}, {14: 48}, {14: 49}], [{1: 0}, {2: 1}, {2: 2}, {3: 3}, {4: 4}, {5: 5}, {5: 6}, {6: 7}, {6: 8}, {6: 9}, {6: 10}, {7: 11}, {7: 12}, {7: 13}, {8: 14}, {8: 15}, {9: 16}, {9: 17}, {9: 18}, {9: 19}, {10: 20}, {10: 21}, {10: 22}, {10: 23}, {10: 24}, {11: 25}, {12: 26}, {12: 27}, {13: 28}, {14: 29}, {14: 30}, {14: 31}, {14: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}, {14: 48}], [{1: 0}, {1: 1}, {2: 2}, {3: 3}, {4: 4}, {4: 5}, {5: 6}, {5: 7}, {5: 8}, {5: 9}, {6: 10}, {6: 11}, {6: 12}, {7: 13}, {7: 14}, {8: 15}, {8: 16}, {9: 17}, {9: 18}, {10: 19}, {10: 20}, {10: 21}, {10: 22}, {10: 23}, {11: 24}, {12: 25}, {12: 26}, {13: 27}, {14: 28}, {14: 29}, {14: 30}, {14: 31}, {14: 32}, {14: 33}, {14: 34}, {14: 35}, {14: 36}, {14: 37}, {14: 38}, {14: 39}, {14: 40}, {14: 41}, {14: 42}, {14: 43}, {14: 44}, {14: 45}, {14: 46}, {14: 47}]]

rv = []
for r in a:
ret = []
for i in [8, 10, 12]:
ret.append(next(d for d in r if i in d))
rv.append(ret)

或者作为一句台词:

rv = [[next(d for d in r if i in d) for i in [8, 10, 12]] for r in a]

输出:

>>> rv
[[{8: 10}, {10: 24}, {12: 30}],
[{8: 9}, {10: 23}, {12: 29}],
[{8: 12}, {10: 22}, {12: 28}],
[{8: 11}, {10: 21}, {12: 27}],
[{8: 10}, {10: 20}, {12: 26}],
[{8: 12}, {10: 19}, {12: 25}],
[{8: 15}, {10: 20}, {12: 29}],
[{8: 14}, {10: 19}, {12: 28}],
[{8: 13}, {10: 18}, {12: 27}],
[{8: 12}, {10: 17}, {12: 26}],
[{8: 14}, {10: 20}, {12: 26}],
[{8: 15}, {10: 19}, {12: 25}]]

关于python - 从二维字典数组中提取特定的键/值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59008772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com