Pythonの内包表記について

# Pythonの内包表記についていろいろ書いてみた。
2重にしたり、3重にしたりすることもできる。
1行でかけるのはとてもメリットがあることだと思う。

- プログラム

```py
list_a = [ i*i for i in range(0,11)]
print(list_a)
```

- 実行結果
```py
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
```


- プログラム
```py
list_b = [ i for i in range(0,20)]
print(list_b)
```

- 実行結果
```py
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
```

- プログラム
```py
list_c = [ i for i in range(0,20,2)]
print(list_c)
```

- 実行結果
```py
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
```

- プログラム
```py
list_d = [ i for i in range(0,30) if i % 2 ]
print(list_d)
```

- 実行結果
```py
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
```
- プログラム
```py
list_e = [[i*j for i in range(1,10)] for j in range(1,10)]
print(list_e)
```

- 実行結果
```py
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 4, 6, 8, 10, 12, 14, 16, 18], [3, 6, 9, 12, 15, 18, 21, 24, 27], [4, 8, 12, 16, 20, 24, 28, 32, 36], [5, 10, 15, 20, 25, 30, 35, 40, 45], [6, 12, 18, 24, 30, 36, 42, 48, 54], [7, 14, 21, 28, 35, 42, 49, 56, 63], [8, 16, 24, 32, 40, 48, 56, 64, 72], [9, 18, 27, 36, 45, 54, 63, 72, 81]]
```

- プログラム

```py
list_f1 = [i*j*k for i in range(0,10) for j in range(0,10) for k in range(0,10)]
print(list_f1)
```

- 実行結果
```
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 0, 14, 28, 42, 56, 70, 84, 98, 112, 126, 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 0, 21, 42, 63, 84, 105, 126, 147, 168, 189, 0, 24, 48, 72, 96, 120, 144, 168, 192, 216, 0, 27, 54, 81, 108, 135, 162, 189, 216, 243, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 0, 24, 48, 72, 96, 120, 144, 168, 192, 216, 0, 28, 56, 84, 112, 140, 168, 196, 224, 252, 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 0, 36, 72, 108, 144, 180, 216, 252, 288, 324, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 0, 35, 70, 105, 140, 175, 210, 245, 280, 315, 0, 40, 80, 120, 160, 200, 240, 280, 320, 360, 0, 45, 90, 135, 180, 225, 270, 315, 360, 405, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 0, 24, 48, 72, 96, 120, 144, 168, 192, 216, 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 0, 36, 72, 108, 144, 180, 216, 252, 288, 324, 0, 42, 84, 126, 168, 210, 252, 294, 336, 378, 0, 48, 96, 144, 192, 240, 288, 336, 384, 432, 0, 54, 108, 162, 216, 270, 324, 378, 432, 486, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 0, 14, 28, 42, 56, 70, 84, 98, 112, 126, 0, 21, 42, 63, 84, 105, 126, 147, 168, 189, 0, 28, 56, 84, 112, 140, 168, 196, 224, 252, 0, 35, 70, 105, 140, 175, 210, 245, 280, 315, 0, 42, 84, 126, 168, 210, 252, 294, 336, 378, 0, 49, 98, 147, 196, 245, 294, 343, 392, 441, 0, 56, 112, 168, 224, 280, 336, 392, 448, 504, 0, 63, 126, 189, 252, 315, 378, 441, 504, 567, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 0, 24, 48, 72, 96, 120, 144, 168, 192, 216, 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 0, 40, 80, 120, 160, 200, 240, 280, 320, 360, 0, 48, 96, 144, 192, 240, 288, 336, 384, 432, 0, 56, 112, 168, 224, 280, 336, 392, 448, 504, 0, 64, 128, 192, 256, 320, 384, 448, 512, 576, 0, 72, 144, 216, 288, 360, 432, 504, 576, 648, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 0, 27, 54, 81, 108, 135, 162, 189, 216, 243, 0, 36, 72, 108, 144, 180, 216, 252, 288, 324, 0, 45, 90, 135, 180, 225, 270, 315, 360, 405, 0, 54, 108, 162, 216, 270, 324, 378, 432, 486, 0, 63, 126, 189, 252, 315, 378, 441, 504, 567, 0, 72, 144, 216, 288, 360, 432, 504, 576, 648, 0, 81, 162, 243, 324, 405, 486, 567, 648, 729]
```



- プログラム
```py
list_f2 = [[[i*j*k for i in range(1,10)] for j in range(1,10)] for k in range(1,10)]
print(list_f2)
```

- 実行結果
```py
[[[1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 4, 6, 8, 10, 12, 14, 16, 18], [3, 6, 9, 12, 15, 18, 21, 24, 27], [4, 8, 12, 16, 20, 24, 28, 32, 36], [5, 10, 15, 20, 25, 30, 35, 40, 45], [6, 12, 18, 24, 30, 36, 42, 48, 54], [7, 14, 21, 28, 35, 42, 49, 56, 63], [8, 16, 24, 32, 40, 48, 56, 64, 72], [9, 18, 27, 36, 45, 54, 63, 72, 81]], [[2, 4, 6, 8, 10, 12, 14, 16, 18], [4, 8, 12, 16, 20, 24, 28, 32, 36], [6, 12, 18, 24, 30, 36, 42, 48, 54], [8, 16, 24, 32, 40, 48, 56, 64, 72], [10, 20, 30, 40, 50, 60, 70, 80, 90], [12, 24, 36, 48, 60, 72, 84, 96, 108], [14, 28, 42, 56, 70, 84, 98, 112, 126], [16, 32, 48, 64, 80, 96, 112, 128, 144], [18, 36, 54, 72, 90, 108, 126, 144, 162]], [[3, 6, 9, 12, 15, 18, 21, 24, 27], [6, 12, 18, 24, 30, 36, 42, 48, 54], [9, 18, 27, 36, 45, 54, 63, 72, 81], [12, 24, 36, 48, 60, 72, 84, 96, 108], [15, 30, 45, 60, 75, 90, 105, 120, 135], [18, 36, 54, 72, 90, 108, 126, 144, 162], [21, 42, 63, 84, 105, 126, 147, 168, 189], [24, 48, 72, 96, 120, 144, 168, 192, 216], [27, 54, 81, 108, 135, 162, 189, 216, 243]], [[4, 8, 12, 16, 20, 24, 28, 32, 36], [8, 16, 24, 32, 40, 48, 56, 64, 72], [12, 24, 36, 48, 60, 72, 84, 96, 108], [16, 32, 48, 64, 80, 96, 112, 128, 144], [20, 40, 60, 80, 100, 120, 140, 160, 180], [24, 48, 72, 96, 120, 144, 168, 192, 216], [28, 56, 84, 112, 140, 168, 196, 224, 252], [32, 64, 96, 128, 160, 192, 224, 256, 288], [36, 72, 108, 144, 180, 216, 252, 288, 324]], [[5, 10, 15, 20, 25, 30, 35, 40, 45], [10, 20, 30, 40, 50, 60, 70, 80, 90], [15, 30, 45, 60, 75, 90, 105, 120, 135], [20, 40, 60, 80, 100, 120, 140, 160, 180], [25, 50, 75, 100, 125, 150, 175, 200, 225], [30, 60, 90, 120, 150, 180, 210, 240, 270], [35, 70, 105, 140, 175, 210, 245, 280, 315], [40, 80, 120, 160, 200, 240, 280, 320, 360], [45, 90, 135, 180, 225, 270, 315, 360, 405]], [[6, 12, 18, 24, 30, 36, 42, 48, 54], [12, 24, 36, 48, 60, 72, 84, 96, 108], [18, 36, 54, 72, 90, 108, 126, 144, 162], [24, 48, 72, 96, 120, 144, 168, 192, 216], [30, 60, 90, 120, 150, 180, 210, 240, 270], [36, 72, 108, 144, 180, 216, 252, 288, 324], [42, 84, 126, 168, 210, 252, 294, 336, 378], [48, 96, 144, 192, 240, 288, 336, 384, 432], [54, 108, 162, 216, 270, 324, 378, 432, 486]], [[7, 14, 21, 28, 35, 42, 49, 56, 63], [14, 28, 42, 56, 70, 84, 98, 112, 126], [21, 42, 63, 84, 105, 126, 147, 168, 189], [28, 56, 84, 112, 140, 168, 196, 224, 252], [35, 70, 105, 140, 175, 210, 245, 280, 315], [42, 84, 126, 168, 210, 252, 294, 336, 378], [49, 98, 147, 196, 245, 294, 343, 392, 441], [56, 112, 168, 224, 280, 336, 392, 448, 504], [63, 126, 189, 252, 315, 378, 441, 504, 567]], [[8, 16, 24, 32, 40, 48, 56, 64, 72], [16, 32, 48, 64, 80, 96, 112, 128, 144], [24, 48, 72, 96, 120, 144, 168, 192, 216], [32, 64, 96, 128, 160, 192, 224, 256, 288], [40, 80, 120, 160, 200, 240, 280, 320, 360], [48, 96, 144, 192, 240, 288, 336, 384, 432], [56, 112, 168, 224, 280, 336, 392, 448, 504], [64, 128, 192, 256, 320, 384, 448, 512, 576], [72, 144, 216, 288, 360, 432, 504, 576, 648]], [[9, 18, 27, 36, 45, 54, 63, 72, 81], [18, 36, 54, 72, 90, 108, 126, 144, 162], [27, 54, 81, 108, 135, 162, 189, 216, 243], [36, 72, 108, 144, 180, 216, 252, 288, 324], [45, 90, 135, 180, 225, 270, 315, 360, 405], [54, 108, 162, 216, 270, 324, 378, 432, 486], [63, 126, 189, 252, 315, 378, 441, 504, 567], [72, 144, 216, 288, 360, 432, 504, 576, 648], [81, 162, 243, 324, 405, 486, 567, 648, 729]]]
```

- プログラム
```py
dict_a = {i:i*2 for i in range(0,20)}
print(dict_a)
```

- 実行結果
```py
{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}
```
- プログラム
```py
dict_b = {i:i*2 for i in range(0,20) if i % 3}
print(dict_b)
```

- 実行結果
```py
{1: 2, 2: 4, 4: 8, 5: 10, 7: 14, 8: 16, 10: 20, 11: 22, 13: 26, 14: 28, 16: 32, 17: 34, 19: 38}
```

- プログラム
```py
dict_c = {i:i*2 for i in range(0,20) if i % 2 or i % 3}
print(dict_c)
```

- 実行結果
```py
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 19: 38}
```

- プログラム
```py
dict_d = {i:i*2 for i in range(0,20) if i % 2 and i % 3}
print(dict_d)
```

- 実行結果
```py
{1: 2, 5: 10, 7: 14, 11: 22, 13: 26, 17: 34, 19: 38}
```



- プログラム
```py
dict_e = { i: { j : j*2 for j in range(0,20)} for i in range(0,20)}
print(dict_e)
```

- 実行結果
```py
{0: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 1: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 2: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 3: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 4: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 5: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 6: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 7: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 8: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 9: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 10: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 11: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 12: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 13: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 14: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 15: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 16: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 17: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 18: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}, 19: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24, 13: 26, 14: 28, 15: 30, 16: 32, 17: 34, 18: 36, 19: 38}}
```