python嵌套列表 - 列表对字典的理解

时间:2014-10-28 22:31:42

标签: python list nested list-comprehension

我正在尝试创建列表的列表理解,如下所示:

list=[{0:[1,2,3]}, {1:[2,3,4]}, {2:[3,4,5]}.....{100:[101,102,103]}]

我尝试创建每个列表。然后每个列表从其键+ 1的值开始:

键0是列表[1,2,3] 第37项清单[38,39,40] 等

无论我怎样尝试,我都无法弄明白。请帮忙!

2 个答案:

答案 0 :(得分:2)

您可以使用以下列表推导来生成此类列表

[{key: list(range(key+1, key+4))} for key in range(101)]

[{0: [1, 2, 3]}, {1: [2, 3, 4]}, {2: [3, 4, 5]}, {3: [4, 5, 6]}, {4: [5, 6, 7]}, {5: [6, 7, 8]}, {6: [7, 8, 9]}, {7: [8, 9, 10]}, {8: [9, 10, 11]}, {9: [10, 11, 12]},
 {10: [11, 12, 13]}, {11: [12, 13, 14]}, {12: [13, 14, 15]}, {13: [14, 15, 16]}, {14: [15, 16, 17]}, {15: [16, 17, 18]}, {16: [17, 18, 19]}, {17: [18, 19, 20]}, {18: [19, 20, 21]}, {19: [20, 21, 22]},
 {20: [21, 22, 23]}, {21: [22, 23, 24]}, {22: [23, 24, 25]}, {23: [24, 25, 26]}, {24: [25, 26, 27]}, {25: [26, 27, 28]}, {26: [27, 28, 29]}, {27: [28, 29, 30]}, {28: [29, 30, 31]}, {29: [30, 31, 32]},
 {30: [31, 32, 33]}, {31: [32, 33, 34]}, {32: [33, 34, 35]}, {33: [34, 35, 36]}, {34: [35, 36, 37]}, {35: [36, 37, 38]}, {36: [37, 38, 39]}, {37: [38, 39, 40]}, {38: [39, 40, 41]}, {39: [40, 41, 42]},
 {40: [41, 42, 43]}, {41: [42, 43, 44]}, {42: [43, 44, 45]}, {43: [44, 45, 46]}, {44: [45, 46, 47]}, {45: [46, 47, 48]}, {46: [47, 48, 49]}, {47: [48, 49, 50]}, {48: [49, 50, 51]}, {49: [50, 51, 52]},
 {50: [51, 52, 53]}, {51: [52, 53, 54]}, {52: [53, 54, 55]}, {53: [54, 55, 56]}, {54: [55, 56, 57]}, {55: [56, 57, 58]}, {56: [57, 58, 59]}, {57: [58, 59, 60]}, {58: [59, 60, 61]}, {59: [60, 61, 62]},
 {60: [61, 62, 63]}, {61: [62, 63, 64]}, {62: [63, 64, 65]}, {63: [64, 65, 66]}, {64: [65, 66, 67]}, {65: [66, 67, 68]}, {66: [67, 68, 69]}, {67: [68, 69, 70]}, {68: [69, 70, 71]}, {69: [70, 71, 72]},
 {70: [71, 72, 73]}, {71: [72, 73, 74]}, {72: [73, 74, 75]}, {73: [74, 75, 76]}, {74: [75, 76, 77]}, {75: [76, 77, 78]}, {76: [77, 78, 79]}, {77: [78, 79, 80]}, {78: [79, 80, 81]}, {79: [80, 81, 82]},
 {80: [81, 82, 83]}, {81: [82, 83, 84]}, {82: [83, 84, 85]}, {83: [84, 85, 86]}, {84: [85, 86, 87]}, {85: [86, 87, 88]}, {86: [87, 88, 89]}, {87: [88, 89, 90]}, {88: [89, 90, 91]}, {89: [90, 91, 92]},
 {90: [91, 92, 93]}, {91: [92, 93, 94]}, {92: [93, 94, 95]}, {93: [94, 95, 96]}, {94: [95, 96, 97]}, {95: [96, 97, 98]}, {96: [97, 98, 99]}, {97: [98, 99, 100]}, {98: [99, 100, 101]}, {99: [100, 101, 102]},
 {100: [101, 102, 103]}]

答案 1 :(得分:0)

print([{i-1:range(i,i+3)} for i in range(1,101)])
[{0: [1, 2, 3]}, {1: [2, 3, 4]}, {2: [3, 4, 5]}, {3: [4, 5, 6]}, {4: [5, 6, 7]}, {5: [6, 7, 8]}, {6: [7, 8, 9]}, {7: [8, 9, 10]}, {8: [9, 10, 11]}, {9: [10, 11, 12]}, {10: [11, 12, 13]}, {11: [12, 13, 14]}, {12: [13, 14, 15]}, {13: [14, 15, 16]}, {14: [15, 16, 17]}, {15: [16, 17, 18]}, {16: [17, 18, 19]}, {17: [18, 19, 20]}, {18: [19, 20, 21]}, {19: [20, 21, 22]}, {20: [21, 22, 23]}, {21: [22, 23, 24]}, {22: [23, 24, 25]}, {23: [24, 25, 26]}, {24: [25, 26, 27]}, {25: [26, 27, 28]}, {26: [27, 28, 29]}, {27: [28, 29, 30]}, {28: [29, 30, 31]}, {29: [30, 31, 32]}, {30: [31, 32, 33]}, {31: [32, 33, 34]}, {32: [33, 34, 35]}, {33: [34, 35, 36]}, {34: [35, 36, 37]}, {35: [36, 37, 38]}, {36: [37, 38, 39]}, {37: [38, 39, 40]}, {38: [39, 40, 41]}, {39: [40, 41, 42]}, {40: [41, 42, 43]}, {41: [42, 43, 44]}, {42: [43, 44, 45]}, {43: [44, 45, 46]}, {44: [45, 46, 47]}, {45: [46, 47, 48]}, {46: [47, 48, 49]}, {47: [48, 49, 50]}, {48: [49, 50, 51]}, {49: [50, 51, 52]}, {50: [51, 52, 53]}, {51: [52, 53, 54]}, {52: [53, 54, 55]}, {53: [54, 55, 56]}, {54: [55, 56, 57]}, {55: [56, 57, 58]}, {56: [57, 58, 59]}, {57: [58, 59, 60]}, {58: [59, 60, 61]}, {59: [60, 61, 62]}, {60: [61, 62, 63]}, {61: [62, 63, 64]}, {62: [63, 64, 65]}, {63: [64, 65, 66]}, {64: [65, 66, 67]}, {65: [66, 67, 68]}, {66: [67, 68, 69]}, {67: [68, 69, 70]}, {68: [69, 70, 71]}, {69: [70, 71, 72]}, {70: [71, 72, 73]}, {71: [72, 73, 74]}, {72: [73, 74, 75]}, {73: [74, 75, 76]}, {74: [75, 76, 77]}, {75: [76, 77, 78]}, {76: [77, 78, 79]}, {77: [78, 79, 80]}, {78: [79, 80, 81]}, {79: [80, 81, 82]}, {80: [81, 82, 83]}, {81: [82, 83, 84]}, {82: [83, 84, 85]}, {83: [84, 85, 86]}, {84: [85, 86, 87]}, {85: [86, 87, 88]}, {86: [87, 88, 89]}, {87: [88, 89, 90]}, {88: [89, 90, 91]}, {89: [90, 91, 92]}, {90: [91, 92, 93]}, {91: [92, 93, 94]}, {92: [93, 94, 95]}, {93: [94, 95, 96]}, {94: [95, 96, 97]}, {95: [96, 97, 98]}, {96: [97, 98, 99]}, {97: [98, 99, 100]}, {98: [99, 100, 101]}]