回答編集履歴

2

コメント

2018/06/24 14:28

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -20,31 +20,33 @@
20
20
 
21
21
  def search_target(lst, target):
22
22
 
23
- h = len(lst)
23
+ h = len(lst) # 高さ
24
24
 
25
- w = len(lst[0])
25
+ w = len(lst[0]) # 幅
26
26
 
27
- t_h = len(target)
27
+ t_h = len(target) # 検索対象の高さ
28
28
 
29
- t_w = len(target[0])
29
+ t_w = len(target[0]) # 検索対象の幅
30
30
 
31
- loop_h = h - t_h + 1
31
+ loop_h = h - t_h + 1 # 縦方向のループ回数(検索対象の高さに被らないところまで+検索対象と同じ高さ分)
32
32
 
33
- loop_w = w - t_w + 1
33
+ loop_w = w - t_w + 1 # 横方向のループ回数
34
34
 
35
35
 
36
36
 
37
- def get_partial(i, j):
37
+ def get_partial(i, j): # indexから部分リストを返す
38
38
 
39
- return [row[j:j+t_w] for row in lst[i:i+t_h]]
39
+ return [row[j:j+t_w] for row in lst[i:i+t_h]] # lst[i:i+t_h]でまず垂直方向に取り出す。取り出した各行を今度はrow[j:j+t_w]で水平方向に切り出す
40
40
 
41
41
 
42
42
 
43
+ # ループ
44
+
43
- for i in range(loop_h):
45
+ for i in range(loop_h):
44
46
 
45
47
  for j in range(loop_w):
46
48
 
47
- if target == get_partial(i,j):
49
+ if target == get_partial(i,j): # 部分リストと検索対象が一致したら位置を返す
48
50
 
49
51
  return (i, j)
50
52
 

1

微妙に簡略化

2018/06/24 14:28

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -44,9 +44,7 @@
44
44
 
45
45
  for j in range(loop_w):
46
46
 
47
- partial = get_partial(i,j)
48
-
49
- if target == partial:
47
+ if target == get_partial(i,j):
50
48
 
51
49
  return (i, j)
52
50