第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

python - 用 numpy 函數(shù)(如 numpy.where)替換多個(gè) if elif

python - 用 numpy 函數(shù)(如 numpy.where)替換多個(gè) if elif

湖上湖 2021-08-14 16:25:29
Radio_index、n_x 和 n_y 是整數(shù)我寫了一個(gè)可以運(yùn)行的 if/elif 代碼。目的是找到 radio_index 的 x,y 位置我能用 np.where 做嗎def radio_index2xy(radio_index,n_x,n_y):     con1 = radio_index <= n_x    con2 = (radio_index > n_x) & (radio_index <= n_x+n_y-1)    con3 = (radio_index > n_x+n_y-1) & (radio_index <= 2*n_x+n_y-2)    con4 = (radio_index > 2*n_x+n_y-2) & (radio_index <= 2*n_x+2*n_y-4)    condlist = [[con1],[con2],[con3],[con4]]    choicelist = [[x_pos = radio_index -1 ,y_pos = 0],\                  [(x_pos = n_x -1),(y_pos = radio_index - n_x)],\                  [(x_pos = (n_x-1)-(radio_index-n_x-n_y+1)),(y_pos = n_y -1)],\                  [(x_pos = 0),(y_pos = 2*n_x+2*n_y-4-radio_index+1)]]    np.select(condlist,choicelist)    return x_pos,y_pos if radio_index <= n_x:    x_pos = radio_index -1    y_pos = 0elif radio_index > n_x and radio_index <= n_x+n_y-1:    x_pos = n_x -1    y_pos = radio_index - n_xelif radio_index > n_x+n_y-1 and radio_index <= 2*n_x+n_y-2:    x_pos = (n_x-1)-(radio_index-n_x-n_y+1)    y_pos = n_y -1elif radio_index > 2*n_x+n_y-2 and radio_index <= 2*n_x+2*n_y-4:    x_pos = 0    y_pos = 2*n_x+2*n_y-4-radio_index+1
查看完整描述

1 回答

?
qq_花開花謝_0

TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊

np.searchsorted 對(duì)于這種邏輯很有用:


def radio_index2xy_v(radio_index, n_x, n_y):

    sgn = np.array([0, 1, 1, -1, -1, 0])

    col = np.array([-1, 1, 0, 1, 0, -1])

    coeffs = np.array([[-1, -1],

                       [0, -1],

                       [-n_x, n_x - 1],

                       [n_y - 1, 2*n_x + n_y - 2],

                       [2*n_x + 2*n_y - 3, 0],

                       [-1, -1]])

    cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])

    idx = cusps.searchsorted(radio_index)

    out = coeffs[idx]

    out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index

    return out

演示:


>>> radio_index2xy_v(np.arange(20), 5, 4)

array([[-1, -1],

       [ 0,  0],

       [ 0,  1],

       [ 0,  2],

       [ 0,  3],

       [ 0,  4],

       [ 1,  4],

       [ 2,  4],

       [ 3,  4],

       [ 3,  3],

       [ 3,  2],

       [ 3,  1],

       [ 3,  0],

       [ 2,  0],

       [ 1,  0],

       [-1, -1],

       [-1, -1],

       [-1, -1],

       [-1, -1],

       [-1, -1]])


查看完整回答
反對(duì) 回復(fù) 2021-08-14
  • 1 回答
  • 0 關(guān)注
  • 344 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)