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

質問編集履歴

1

コードにコメントを追加して読みやすくしました。

2021/12/28 09:05

投稿

Ralutet
Ralutet

スコア0

title CHANGED
File without changes
body CHANGED
@@ -17,43 +17,43 @@
17
17
 
18
18
  ```Python
19
19
  import numpy as np
20
- lst = np.array([[0,1,3,3,0],[1,0,3,0,0],[1,0,3,3,0],[1,3,3,0,0],[1,3,0,0,0]])
20
+ lst = np.array([[0,1,3,3,0],[1,0,3,0,0],[1,0,3,3,0],[1,3,3,0,0],[1,3,0,0,0]])#質問の一番上に書いてる配列と同じ
21
- lst2 = np.zeros((10,3))
21
+ lst2 = np.zeros((10,3))#後に、質問の上から二番目に書いてる配列と同じになる
22
- for i in range(len(lst)):
22
+ for i in range(len(lst)):#0つ目から試す(3にする前のもの)
23
23
  lst2_temp = []
24
- for j in range(len(lst)):
24
+ for j in range(len(lst)):#0つ目から試す(3にした後のもの)
25
- if int(np.count_nonzero(lst[j] > 0)) - int(np.count_nonzero(lst[i] > 0)) == 1:
25
+ if int(np.count_nonzero(lst[j] > 0)) - int(np.count_nonzero(lst[i] > 0)) == 1:#それらの配列の0の数が一つ違う = 3が一つ増えた
26
- if np.count_nonzero(np.logical_and(lst[j]-lst[i] != 3, lst[j]-lst[i] != 0)) == 0:
26
+ if np.count_nonzero(np.logical_and(lst[j]-lst[i] != 3, lst[j]-lst[i] != 0)) == 0:#それらの配列の差をとったときに、3でも0でもないところがない = 3が一つ増えた以外の変化はない
27
- lst2_temp.append(j)
27
+ lst2_temp.append(j) #3にしたあとのものの番号を記録する
28
- for j in range(len(lst2[0])-len(lst2_temp)):
28
+ for j in range(len(lst2[0])-len(lst2_temp)):#リストの長さを3に調整するためのもの
29
29
  lst2_temp.append(0)
30
- lst2[i] = np.array(lst2_temp)
30
+ lst2[i] = np.array(lst2_temp)#これで、質問の上から二番目に書いてる配列と同じになる
31
31
  ```
32
32
 
33
33
  ### 書いたコード(print込)
34
34
 
35
35
  ```Python
36
36
  import numpy as np
37
- lst = np.array([[0,1,3,3,0],[1,0,3,0,0],[1,0,3,3,0],[1,3,3,0,0],[1,3,0,0,0]])
37
+ lst = np.array([[0,1,3,3,0],[1,0,3,0,0],[1,0,3,3,0],[1,3,3,0,0],[1,3,0,0,0]])#質問の一番上に書いてる配列と同じ
38
- lst2 = np.zeros((10,3))
38
+ lst2 = np.zeros((10,3))#後に、質問の上から二番目に書いてる配列と同じになる
39
39
  print(lst)
40
- for i in range(len(lst)):
40
+ for i in range(len(lst)):#0つ目から試す(3にする前のもの)
41
41
  lst2_temp = []
42
42
  print()
43
43
  print("i:",lst[i])
44
- for j in range(len(lst)):
44
+ for j in range(len(lst)):#0つ目から試す(3にした後のもの)
45
45
  print("j:",lst[j])
46
46
  print("i_num:",np.count_nonzero(lst[i] > 0))
47
47
  print("j_num:",np.count_nonzero(lst[j] > 0))
48
- if int(np.count_nonzero(lst[j] > 0)) - int(np.count_nonzero(lst[i] > 0)) == 1:
48
+ if int(np.count_nonzero(lst[j] > 0)) - int(np.count_nonzero(lst[i] > 0)) == 1:#それらの配列の0の数が一つ違う = 3が一つ増えた
49
49
  print(lst[j]-lst[i])
50
- if np.count_nonzero(np.logical_and(lst[j]-lst[i] != 3, lst[j]-lst[i] != 0)) == 0:
50
+ if np.count_nonzero(np.logical_and(lst[j]-lst[i] != 3, lst[j]-lst[i] != 0)) == 0:#それらの配列の差をとったときに、3でも0でもないところがない = 3が一つ増えた以外の変化はない
51
51
  print(j)
52
- lst2_temp.append(j)
52
+ lst2_temp.append(j)#3にしたあとのものの番号を記録する
53
- for j in range(len(lst2[0])-len(lst2_temp)):
53
+ for j in range(len(lst2[0])-len(lst2_temp)):#リストの長さを3に調整するためのもの
54
54
  lst2_temp.append(0)
55
55
  print("lst2_temp:",lst2_temp)
56
- lst2[i] = np.array(lst2_temp)
56
+ lst2[i] = np.array(lst2_temp)#これで、質問の上から二番目に書いてる配列と同じになる
57
57
  print(lst2)
58
58
  ```
59
59