回答編集履歴
2
追記
test
CHANGED
@@ -35,3 +35,39 @@
|
|
35
35
|
{1: ['東京', '神奈川', '千葉'], 2: ['群馬', '茨城', '山梨'], 3: ['長野', '栃木'], 4: ['愛知']}
|
36
36
|
|
37
37
|
```
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
追記
|
42
|
+
|
43
|
+
---
|
44
|
+
|
45
|
+
もしキーが『数値』ではなく『数字』なら。[Wandbox](https://wandbox.org/permlink/0y83iuDNqbBxvKjN)
|
46
|
+
|
47
|
+
```Python
|
48
|
+
|
49
|
+
lists = ['1', '東京', '神奈川', '千葉', '2', '群馬', '茨城', '山梨', '3', '長野', '栃木', '4', '愛知']
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
dst = {}
|
54
|
+
|
55
|
+
for elem in lists:
|
56
|
+
|
57
|
+
try:
|
58
|
+
|
59
|
+
elem = int(elem)
|
60
|
+
|
61
|
+
dst[elem] = []
|
62
|
+
|
63
|
+
pre_key = elem
|
64
|
+
|
65
|
+
except ValueError:
|
66
|
+
|
67
|
+
dst[pre_key].append(elem)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
print(dst)
|
72
|
+
|
73
|
+
```
|
1
追記
test
CHANGED
@@ -6,25 +6,21 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
|
9
|
+
dst = {}
|
10
|
-
|
11
|
-
values = []
|
12
10
|
|
13
11
|
for elem in lists:
|
14
12
|
|
15
13
|
if isinstance(elem, int):
|
16
14
|
|
17
|
-
|
15
|
+
dst[elem] = []
|
18
16
|
|
19
|
-
|
17
|
+
pre_key = elem
|
20
18
|
|
21
19
|
else:
|
22
20
|
|
23
|
-
|
21
|
+
dst[pre_key].append(elem)
|
24
22
|
|
25
23
|
|
26
|
-
|
27
|
-
dst = dict(zip(keys, values))
|
28
24
|
|
29
25
|
print(dst)
|
30
26
|
|
@@ -32,7 +28,7 @@
|
|
32
28
|
|
33
29
|
|
34
30
|
|
35
|
-
**実行結果** [Wandbox](https://wandbox.org/permlink/b
|
31
|
+
**実行結果** [Wandbox](https://wandbox.org/permlink/d4yBWbZMijSWLhAK)
|
36
32
|
|
37
33
|
```
|
38
34
|
|