teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

f

2020/10/20 10:09

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -2,9 +2,9 @@
2
2
  ```
3
3
 
4
4
  result = [
5
- {'name': 'yamada', 'weight': '72', 'height':180},
5
+ {'name': 'yamada', 'weight': '72', 'height':'180'},
6
- {'name': 'yamada', 'weight': '73', 'height':180},
6
+ {'name': 'yamada', 'weight': '73', 'height':'180'},
7
- {'name': 'yamada', 'weight': '75', 'height':180},
7
+ {'name': 'yamada', 'weight': '75', 'height':'180'},
8
8
  ]
9
9
 
10
10
  ```
@@ -54,15 +54,15 @@
54
54
  "DataList": [
55
55
  {
56
56
  "weight": "72",
57
- "height": 180
57
+ "height": "180"
58
58
  },
59
59
  {
60
- "weight": "74",
60
+ "weight": "73",
61
- "height": 182
61
+ "height": "180"
62
62
  },
63
63
  {
64
- "weight": "54",
64
+ "weight": "75",
65
- "height": 170
65
+ "height": "180"
66
66
  }
67
67
  ]
68
68
  }

2

2020/10/20 10:09

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -3,8 +3,8 @@
3
3
 
4
4
  result = [
5
5
  {'name': 'yamada', 'weight': '72', 'height':180},
6
- {'name': 'yamada', 'weight': '74', 'height':182},
6
+ {'name': 'yamada', 'weight': '73', 'height':180},
7
- {'name': 'yamada', 'weight': '54', 'height':170},
7
+ {'name': 'yamada', 'weight': '75', 'height':180},
8
8
  ]
9
9
 
10
10
  ```

1

修正

2020/10/20 10:08

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -2,34 +2,34 @@
2
2
  ```
3
3
 
4
4
  result = [
5
- {'name': 'yamada', 'weight': '72', 'height':180},
5
+ {'name': 'yamada', 'weight': '72', 'height':180},
6
- {'name': 'yamada', 'weight': '74', 'height':182},
6
+ {'name': 'yamada', 'weight': '74', 'height':182},
7
- {'name': 'satou', 'weight': '54', 'height':170},
7
+ {'name': 'yamada', 'weight': '54', 'height':170},
8
- {'name': 'satou', 'weight': '58', 'height':172},
9
- {'name': 'tanaka', 'weight': '42', 'height':149}
10
8
  ]
9
+
11
10
  ```
12
11
 
13
12
 
14
13
  処理用プログラムconvert関数は以下の通り
15
14
  ```
15
+
16
16
  def convert(src, keywords):
17
- # src :変換元データ
17
+ # 結果格納用辞書
18
- # keywords : 抽出するカラムを指定したリスト
19
-
20
- dest = dict() # 結果格納用の辞書
18
+ dest = dict()
21
19
  for src_item in src:
22
20
  # まずキーを登録する。
23
21
  name = src_item.get('name')
22
+ dest.setdefault("name", name)
24
- dest.setdefault(name, {"DataList":[]})
23
+ dest.setdefault("DataList", [])
25
24
  # DataListに格納する個別データ
26
25
  datalist_item = dict()
27
26
  for k, v in src_item.items():
28
27
  if k in keywords:
29
28
  datalist_item.update({k: v})
30
29
  if datalist_item:
31
- dest[name]["DataList"].append(datalist_item)
30
+ dest["DataList"].append(datalist_item)
32
31
  return dest
32
+
33
33
  ```
34
34
 
35
35
 
@@ -50,37 +50,20 @@
50
50
  出力結果
51
51
  ```
52
52
  {
53
- "yamada": {
53
+ "name": "yamada",
54
- "DataList": [
54
+ "DataList": [
55
- {
55
+ {
56
- "weight": "72",
56
+ "weight": "72",
57
- "height": 180
57
+ "height": 180
58
- },
58
+ },
59
- {
59
+ {
60
- "weight": "74",
60
+ "weight": "74",
61
- "height": 182
61
+ "height": 182
62
+ },
63
+ {
64
+ "weight": "54",
65
+ "height": 170
62
- }
66
+ }
63
- ]
67
+ ]
64
- },
65
- "satou": {
66
- "DataList": [
67
- {
68
- "weight": "54",
69
- "height": 170
70
- },
71
- {
72
- "weight": "58",
73
- "height": 172
74
- }
75
- ]
76
- },
77
- "tanaka": {
78
- "DataList": [
79
- {
80
- "weight": "42",
81
- "height": 149
82
- }
83
- ]
84
- }
85
68
  }
86
69
  ```