我怎样才能缩短这个 if、elif、else 语句。重点是提高效率并更快地执行相同的任务。
整个片段位于两个长 for i 循环内,我很确定这就是导致代码速度减慢的原因。
if values[i + (j * height)] <= (255/67)*1:
output = output + str(characters[66])
elif values[i + (j * height)] <= (255/67)*2:
output = output + str(characters[65])
elif values[i + (j * height)] <= (255/67)*3:
output = output + str(characters[64])
elif values[i + (j * height)] <= (255/67)*4:
output = output + str(characters[63])
elif values[i + (j * height)] <= (255/67)*5:
output = output + str(characters[62])
elif values[i + (j * height)] <= (255/67)*6:
output = output + str(characters[61])
elif values[i + (j * height)] <= (255/67)*7:
output = output + str(characters[60])
elif values[i + (j * height)] <= (255/67)*8:
output = output + str(characters[59])
elif values[i + (j * height)] <= (255/67)*9:
output = output + str(characters[58])
elif values[i + (j * height)] <= (255/67)*10:
output = output + str(characters[57])
elif values[i + (j * height)] <= (255/67)*11:
output = output + str(characters[56])
elif values[i + (j * height)] <= (255/67)*12:
output = output + str(characters[55])
elif values[i + (j * height)] <= (255/67)*13:
output = output + str(characters[54])
elif values[i + (j * height)] <= (255/67)*14:
output = output + str(characters[53])
elif values[i + (j * height)] <= (255/67)*15:
output = output + str(characters[52])
elif values[i + (j * height)] <= (255/67)*16:
output = output + str(characters[51])
elif values[i + (j * height)] <= (255/67)*17:
output = output + str(characters[50])
elif values[i + (j * height)] <= (255/67)*18:
output = output + str(characters[49])
elif values[i + (j * height)] <= (255/67)*19:
output = output + str(characters[48])
elif values[i + (j * height)] <= (255/67)*20:
output = output + str(characters[47])
elif values[i + (j * height)] <= (255/67)*21:
output = output + str(characters[46])
elif values[i + (j * height)] <= (255/67)*22:
output = output + str(characters[45])
elif values[i + (j * height)] <= (255/67)*23:
output = output + str(characters[44])
elif values[i + (j * height)] <= (255/67)*24:
output = output + str(characters[43])
elif values[i + (j * height)] <= (255/67)*25:
output = output + str(characters[42])
elif values[i + (j * height)] <= (255/67)*26:
output = output + str(characters[41])
elif values[i + (j * height)] <= (255/67)*27:
output = output + str(characters[40])
elif values[i + (j * height)] <= (255/67)*28:
output = output + str(characters[39])
elif values[i + (j * height)] <= (255/67)*29:
output = output + str(characters[38])
elif values[i + (j * height)] <= (255/67)*30:
output = output + str(characters[37])
elif values[i + (j * height)] <= (255/67)*31:
output = output + str(characters[36])
elif values[i + (j * height)] <= (255/67)*32:
output = output + str(characters[35])
elif values[i + (j * height)] <= (255/67)*33:
output = output + str(characters[34])
elif values[i + (j * height)] <= (255/67)*34:
output = output + str(characters[33])
elif values[i + (j * height)] <= (255/67)*35:
output = output + str(characters[32])
elif values[i + (j * height)] <= (255/67)*36:
output = output + str(characters[31])
elif values[i + (j * height)] <= (255/67)*37:
output = output + str(characters[30])
elif values[i + (j * height)] <= (255/67)*38:
output = output + str(characters[29])
elif values[i + (j * height)] <= (255/67)*39:
output = output + str(characters[28])
elif values[i + (j * height)] <= (255/67)*40:
output = output + str(characters[27])
elif values[i + (j * height)] <= (255/67)*41:
output = output + str(characters[26])
elif values[i + (j * height)] <= (255/67)*42:
output = output + str(characters[25])
elif values[i + (j * height)] <= (255/67)*43:
output = output + str(characters[24])
elif values[i + (j * height)] <= (255/67)*44:
output = output + str(characters[23])
elif values[i + (j * height)] <= (255/67)*45:
output = output + str(characters[22])
elif values[i + (j * height)] <= (255/67)*46:
output = output + str(characters[21])
elif values[i + (j * height)] <= (255/67)*47:
output = output + str(characters[20])
elif values[i + (j * height)] <= (255/67)*48:
output = output + str(characters[19])
elif values[i + (j * height)] <= (255/67)*49:
output = output + str(characters[18])
elif values[i + (j * height)] <= (255/67)*50:
output = output + str(characters[17])
elif values[i + (j * height)] <= (255/67)*51:
output = output + str(characters[16])
elif values[i + (j * height)] <= (255/67)*52:
output = output + str(characters[15])
elif values[i + (j * height)] <= (255/67)*53:
output = output + str(characters[14])
elif values[i + (j * height)] <= (255/67)*54:
output = output + str(characters[13])
elif values[i + (j * height)] <= (255/67)*55:
output = output + str(characters[12])
elif values[i + (j * height)] <= (255/67)*56:
output = output + str(characters[11])
elif values[i + (j * height)] <= (255/67)*57:
output = output + str(characters[10])
elif values[i + (j * height)] <= (255/67)*58:
output = output + str(characters[9])
elif values[i + (j * height)] <= (255/67)*59:
output = output + str(characters[8])
elif values[i + (j * height)] <= (255/67)*60:
output = output + str(characters[7])
elif values[i + (j * height)] <= (255/67)*61:
output = output + str(characters[6])
elif values[i + (j * height)] <= (255/67)*62:
output = output + str(characters[5])
elif values[i + (j * height)] <= (255/67)*63:
output = output + str(characters[4])
elif values[i + (j * height)] <= (255/67)*64:
output = output + str(characters[3])
elif values[i + (j * height)] <= (255/67)*65:
output = output + str(characters[2])
elif values[i + (j * height)] <= (255/67)*66:
output = output + str(characters[1])
else:
output = output + str(characters[0])
我只是不知道如何查找某个值是否在某个范围内。有人建议使用“
list = {0:"abc", 1:"efg, 2:...}
但我不确定如何检查该值是否在范围内
如果我没有弄错你的例子,那么可以使用数学方法来优化代码。您可以直接计算索引,而不是使用 elif 检查每个范围。
factor = 255 / 67
for j in range(height):
for i in range(width): # Assuming width is defined
# Calculate the value index directly
value = values[i + (j * height)]
index = 66 - int(value // factor)
output += str(characters[index])