前提
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
def labeling_4nn(img): h, w = img.shape label = np.zeros((h, w), dtype=int) label[img > 0] = 1 # look up table LUT = [0 for _ in range(h * w)] n = 1 for y in range(h): for x in range(w): # skip black pixel if label[y, x] == 0: continue # get above pixel c3 = label[max(y-1,0), x] # get left pixel c5 = label[y, max(x-1,0)] # if not labeled if c3 < 2 and c5 < 2: # labeling n += 1 label[y, x] = n else: # replace min label index _vs = [c3, c5] vs = [a for a in _vs if a > 1] v = min(vs) label[y, x] = v minv = v for _v in vs: if LUT[_v] != 0: minv = min(minv, LUT[_v]) for _v in vs: LUT[_v] = minv count = 1 # integrate index of look up table for l in range(2, n+1): flag = True for i in range(n+1): if LUT[i] == l: if flag: count += 1 flag = False LUT[i] = count # draw color COLORS = [[0, 0, 255], [0, 255, 0], [255, 0, 0], [255, 255, 0]] out = np.zeros((h, w, 3), dtype=np.uint8) for i, lut in enumerate(LUT[2:]): out[label == (i+2)] = COLORS[lut-2] return out out = labeling_4nn(img_seg) plt.subplot(1, 2, 1) plt.title("input") plt.imshow(img_seg, cmap="gray") plt.subplot(1, 2, 2) plt.title("output") plt.imshow(out) plt.show()
試したこと
下記URLで調べました。かっこの左に文字がくるパターンがなかったです。
https://www.tohoho-web.com/python/list.html
まだ回答がついていません
会員登録して回答してみよう