前提
pythonで大かっこの使い方が知りたいです。
大かっこはリストで使うという認識です。
参考
https://www.tohoho-web.com/python/list.html
実現したいこと
label[y, x]
という感じの大かっこの左に文字がくる場合の大かっこの意味を知りたいです。
以下のサイトで勉強中に出てきたので私の知っている使い方に該当しなかったため、気になりました。
https://github.com/yoyoyo-yo/Gasyori100knock/tree/master/questions/question_51_60#q55-%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%83%9E%E3%83%83%E3%83%81%E3%83%B3%E3%82%B0-sad
該当のソースコード
python
1def labeling_4nn(img): 2 h, w = img.shape 3 4 label = np.zeros((h, w), dtype=int) 5 label[img > 0] = 1 6 7 # look up table 8 LUT = [0 for _ in range(h * w)] 9 10 n = 1 11 12 for y in range(h): 13 for x in range(w): 14 # skip black pixel 15 if label[y, x] == 0: 16 continue 17 18 # get above pixel 19 c3 = label[max(y-1,0), x] 20 21 # get left pixel 22 c5 = label[y, max(x-1,0)] 23 24 # if not labeled 25 if c3 < 2 and c5 < 2: 26 # labeling 27 n += 1 28 label[y, x] = n 29 else: 30 # replace min label index 31 _vs = [c3, c5] 32 vs = [a for a in _vs if a > 1] 33 v = min(vs) 34 label[y, x] = v 35 36 minv = v 37 for _v in vs: 38 if LUT[_v] != 0: 39 minv = min(minv, LUT[_v]) 40 for _v in vs: 41 LUT[_v] = minv 42 43 count = 1 44 45 # integrate index of look up table 46 for l in range(2, n+1): 47 flag = True 48 for i in range(n+1): 49 if LUT[i] == l: 50 if flag: 51 count += 1 52 flag = False 53 LUT[i] = count 54 55 # draw color 56 COLORS = [[0, 0, 255], [0, 255, 0], [255, 0, 0], [255, 255, 0]] 57 out = np.zeros((h, w, 3), dtype=np.uint8) 58 59 for i, lut in enumerate(LUT[2:]): 60 out[label == (i+2)] = COLORS[lut-2] 61 62 return out 63 64out = labeling_4nn(img_seg) 65 66plt.subplot(1, 2, 1) 67plt.title("input") 68plt.imshow(img_seg, cmap="gray") 69plt.subplot(1, 2, 2) 70plt.title("output") 71plt.imshow(out) 72plt.show()
試したこと
下記URLで調べました。かっこの左に文字がくるパターンがなかったです。
https://www.tohoho-web.com/python/list.html
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。