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

質問編集履歴

4

2021/05/07 08:13

投稿

gottadiveintopy
gottadiveintopy

スコア736

title CHANGED
File without changes
body CHANGED
@@ -29,7 +29,7 @@
29
29
  ### 補足
30
30
 
31
31
  - 与えられる辞書に含まれる`unnecessary_keys`以外のkeyは一つか二つである事が多いです。(つまり`sample_dict`のような物が多い)
32
- - `unnecessary_keys`は上に書いた内容で常に固定されています。
32
+ - ~~`unnecessary_keys`は上に書いた内容で常に固定されています。~~ 除きたいkeyは上に書いた6つで固定されています。
33
33
 
34
34
 
35
35
  ### 環境
@@ -38,7 +38,7 @@
38
38
 
39
39
  # 追記
40
40
 
41
- 得られた回答を元に以下の5つも比較対象に加えた結果
41
+ 得られた回答を元に以下の6つも比較対象に加えた結果
42
42
 
43
43
  ```
44
44
  unnecessary_keys = ('s', 'd', 't', 'step', 'duration', 'transition', )

3

比較結果の更新

2021/05/07 08:13

投稿

gottadiveintopy
gottadiveintopy

スコア736

title CHANGED
File without changes
body CHANGED
@@ -63,7 +63,7 @@
63
63
 
64
64
 
65
65
  def method_4a(d):
66
- '''ppaulさんのmethod_4に少し手を加えた物'''
66
+ '''ppaulさんのmethod_4'''
67
67
  d = d.copy()
68
68
  for key in unnecessary_keys:
69
69
  if key in d:
@@ -88,22 +88,31 @@
88
88
  if key in d:
89
89
  del d[key]
90
90
  return d
91
+
92
+ def method_6(d):
93
+ '''quickquipさんの方法'''
94
+ d = d.copy()
95
+ for key in intersection(d):
96
+ del d[key]
97
+ return d
91
98
  ```
92
99
 
93
100
  ```
94
- 1 : 5.916446945004282
101
+ 1 : 6.086940804998449
95
- 2 : 7.3307233180021285
102
+ 2 : 7.495010836995789
96
- 3a: 5.222118170997419
103
+ 3a: 5.200945402000798
97
- 3b: 5.047232527002052
104
+ 3b: 5.059846481999557
98
- 4a: 5.437795748002827
99
- 4b: 5.435121403999801
105
+ 4a: 5.46594832399569
106
+ 4b: 5.468696205003653
100
- 5 : 5.131977698998526
107
+ 5 : 5.198188450005546
108
+ 6 : 4.907909608999034
101
109
  ```
102
110
 
103
111
  以下の順となりました。[code全体](https://gist.github.com/gottadiveintopython/04fce1af28eb66d1053ff191f5b616ce)
104
112
 
113
+ 1. `method_6`
105
114
  1. `method_3b`と`method_5` (順位は不安定。`method_3b`の方が速いことが多い)
106
- 2. `method_3a`
115
+ 1. `method_3a`
107
- 3. `method_4a`と`method_4b` (順位は不安定)
116
+ 1. `method_4a`と`method_4b` (順位は不安定)
108
- 4. `method_1`
117
+ 1. `method_1`
109
- 5. `method_2`
118
+ 1. `method_2`

2

回答を元に比較結果を更新

2021/05/07 07:18

投稿

gottadiveintopy
gottadiveintopy

スコア736

title CHANGED
File without changes
body CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  # 追記
40
40
 
41
- ppaulさんの回答を元に以下の4つも比較対象に加えた
41
+ 得られた回答を元に以下の5つも比較対象に加えた結果
42
42
 
43
43
  ```
44
44
  unnecessary_keys = ('s', 'd', 't', 'step', 'duration', 'transition', )
@@ -46,6 +46,7 @@
46
46
 
47
47
 
48
48
  def method_3a(d):
49
+ '''ppaulさんのmethod_3に少し手を加えた物'''
49
50
  d = d.copy()
50
51
  for key in intersection(d):
51
52
  d.__delitem__(key)
@@ -53,6 +54,7 @@
53
54
 
54
55
 
55
56
  def method_3b(d):
57
+ '''method_3aの'd.__delitem__'をloopの外に出した物'''
56
58
  d = d.copy()
57
59
  delitem = d.__delitem__
58
60
  for key in intersection(d):
@@ -61,6 +63,7 @@
61
63
 
62
64
 
63
65
  def method_4a(d):
66
+ '''ppaulさんのmethod_4に少し手を加えた物'''
64
67
  d = d.copy()
65
68
  for key in unnecessary_keys:
66
69
  if key in d:
@@ -69,22 +72,38 @@
69
72
 
70
73
 
71
74
  def method_4b(d):
75
+ '''method_4aの'd.__delitem__'をloopの外に出した物'''
72
76
  d = d.copy()
73
77
  delitem = d.__delitem__
74
78
  for key in unnecessary_keys:
75
79
  if key in d:
76
80
  delitem(key)
77
81
  return d
82
+
83
+
84
+ def method_5(d):
85
+ '''quickquipさんの方法'''
86
+ d = d.copy()
87
+ for key in unnecessary_keys:
88
+ if key in d:
89
+ del d[key]
90
+ return d
78
91
  ```
79
92
 
80
93
  ```
81
- 1 : 5.862659796002845
94
+ 1 : 5.916446945004282
82
- 2 : 7.536483085001237
95
+ 2 : 7.3307233180021285
83
- 3a: 5.1854341859943816
96
+ 3a: 5.222118170997419
84
- 3b: 5.132281307000085
97
+ 3b: 5.047232527002052
98
+ 4a: 5.437795748002827
85
- 4a: 5.413436234994151
99
+ 4b: 5.435121403999801
86
- 4b: 5.490977667999687
100
+ 5 : 5.131977698998526
87
101
  ```
88
102
 
89
- `method_3b` -> `method_3a` -> `method_4a`と`method_4b` -> `method_1` -> `method_2` の順になりました。(`method_4a`と`method_4b`の順位は安定せず)
90
- [code全体](https://gist.github.com/gottadiveintopython/04fce1af28eb66d1053ff191f5b616ce)
103
+ 以下の順となりました。[code全体](https://gist.github.com/gottadiveintopython/04fce1af28eb66d1053ff191f5b616ce)
104
+
105
+ 1. `method_3b`と`method_5` (順位は不安定。`method_3b`の方が速いことが多い)
106
+ 2. `method_3a`
107
+ 3. `method_4a`と`method_4b` (順位は不安定)
108
+ 4. `method_1`
109
+ 5. `method_2`

1

教えてもらった方法との比較結果を追記

2021/05/07 06:44

投稿

gottadiveintopy
gottadiveintopy

スコア736

title CHANGED
File without changes
body CHANGED
@@ -34,4 +34,57 @@
34
34
 
35
35
  ### 環境
36
36
 
37
- CPython 3.7.1
37
+ CPython 3.7.1
38
+
39
+ # 追記
40
+
41
+ ppaulさんの回答を元に以下の4つも比較対象に加えた所
42
+
43
+ ```
44
+ unnecessary_keys = ('s', 'd', 't', 'step', 'duration', 'transition', )
45
+ intersection = set(unnecessary_keys).intersection
46
+
47
+
48
+ def method_3a(d):
49
+ d = d.copy()
50
+ for key in intersection(d):
51
+ d.__delitem__(key)
52
+ return d
53
+
54
+
55
+ def method_3b(d):
56
+ d = d.copy()
57
+ delitem = d.__delitem__
58
+ for key in intersection(d):
59
+ delitem(key)
60
+ return d
61
+
62
+
63
+ def method_4a(d):
64
+ d = d.copy()
65
+ for key in unnecessary_keys:
66
+ if key in d:
67
+ d.__delitem__(key)
68
+ return d
69
+
70
+
71
+ def method_4b(d):
72
+ d = d.copy()
73
+ delitem = d.__delitem__
74
+ for key in unnecessary_keys:
75
+ if key in d:
76
+ delitem(key)
77
+ return d
78
+ ```
79
+
80
+ ```
81
+ 1 : 5.862659796002845
82
+ 2 : 7.536483085001237
83
+ 3a: 5.1854341859943816
84
+ 3b: 5.132281307000085
85
+ 4a: 5.413436234994151
86
+ 4b: 5.490977667999687
87
+ ```
88
+
89
+ `method_3b` -> `method_3a` -> `method_4a`と`method_4b` -> `method_1` -> `method_2` の順になりました。(`method_4a`と`method_4b`の順位は安定せず)
90
+ [code全体](https://gist.github.com/gottadiveintopython/04fce1af28eb66d1053ff191f5b616ce)