在Python dict中使代码更快

时间:2016-03-11 11:56:38

标签: python

我有这段代码:

if pair.direction == "F1R2" or pair.direction == "R2F1":
    key = float(str(pair.r1.reference_id)+str(orient[pair.direction])+"."+str(pair.r1.pos))
    value = float(str(pair.r2.reference_id)+str(orient[pair.direction])+"."+str(pair.r2.pos+pair.frside))
elif pair.direction == "F1F2" or pair.direction == "F2F1":
    key = float(str(pair.r1.reference_id)+str(orient[pair.direction])+"."+str(pair.r1.pos))
    value = float(str(pair.r2.reference_id)+str(orient[pair.direction])+"."+str(pair.r2.pos))
elif pair.direction == "R1R2" or pair.direction == "R2R1":
    key = float(str(pair.r1.reference_id)+str(orient[pair.direction])+"."+str(pair.r1.pos+pair.frside))
    value = float(str(pair.r2.reference_id)+str(orient[pair.direction])+"."+str(pair.r2.pos+pair.frside))
else: #F2R1 or R1F2
    key = float(str(pair.r1.reference_id)+str(orient[pair.direction])+"."+str(pair.r1.pos+pair.frside))
    value = float(str(pair.r2.reference_id)+str(orient[pair.direction])+"."+str(pair.r2.pos))
if pair.r1.reference_id in dupes:
    if key in dupes[pair.r1.reference_id]:
        #duplicate!!!
        outputs["duplicates"].put([attr1,attr2])
        continue
    else:
        dupes[pair.r1.reference_id][key] = value
else: #not a dup
    dupes[pair.r1.reference_id] = {key: value}

在字典dupes中,它可能包含~100M密钥。您是否知道搜索或关联keyvalue变量的更快捷方式? keyvalue个变量都是floats

0 个答案:

没有答案