回答編集履歴
1
追記
test
CHANGED
@@ -43,3 +43,43 @@
|
|
43
43
|
|
44
44
|
|
45
45
|
```
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
リスト内包表記なんかを通常のループに分解する場合は、.append()等で要素を追加することを忘れないようにしておくと良いです。
|
50
|
+
|
51
|
+
今回の例は複雑ですが、以下のようにしてpprintで見やすく表示したり少しづつ式を分解していくと途中の経過がわかり、仕組みの理解につながります。
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
```python
|
56
|
+
|
57
|
+
import itertools
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
a = [2, 5, 7, 3, 6]
|
62
|
+
|
63
|
+
b = [1, 3, 6, 8, 4]
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
from pprint import pprint
|
68
|
+
|
69
|
+
# もともとの
|
70
|
+
|
71
|
+
pprint([(sum([dict(zip(a, b))[v] for i, v in enumerate(x) if i > a.index(v)]), list(x)) for x in itertools.permutations(a)])
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
# sum部分をはずす
|
76
|
+
|
77
|
+
pprint([([dict(zip(a, b))[v] for i, v in enumerate(x) if i > a.index(v)], list(x)) for x in itertools.permutations(a)])
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
# [v]などの辞書キー部分をはずす
|
82
|
+
|
83
|
+
pprint([([dict(zip(a, b)) for i, v in enumerate(x) if i > a.index(v)], list(x)) for x in itertools.permutations(a)])
|
84
|
+
|
85
|
+
```
|