0

我想知道我是否可以使用numpy加速此代码...

代码实际上正在运行,但我知道使用 np.where 可以做得更好,我尝试过但没有成功:)

对于每个同步位置,我想将第一个位置('000','001'...)上的字符串与变量综合症(强制转换为字符串)进行比较,并在匹配时获得第二个位置的 int

就像如果我有一个综合症'100'我会得到 4 所以我知道我必须翻转 8 位代码字中的第 4 位

def recover_data(noisy_data):

syn=[['000','none'],['001',6],['010',5],['011',3],['100',4],['101',0],['110',1],['111',2]]

for ix in range(noisy_data.shape[0]):
    unflip=0    #index that will be flipped

    for jx in range(len(syn)):
        if(syn[jx][0] == ''.join(syndrome.astype('str'))):
            unflip = syn[jx][1]
    if(str(unflip)!='none'):
        noisy_data[ix,unflip]=1-noisy_data[ix,unflip]
4

1 回答 1

0

看起来dictionary会有所帮助

syn=dict([['000','none'],['001',6],['010',5],['011',3],['100',4],['101',0],['110',1],['111',2]])

syn

{'000': 'none',
 '001': 6,
 '010': 5,
 '011': 3,
 '100': 4,
 '101': 0,
 '110': 1,
 '111': 2}

syn.get('011')  # .get(key) will return None if the key isn't in the dict

3
于 2017-12-23T06:34:06.077 回答