我如何将此变量中的Int添加到此列表中的每个int

时间:2016-04-25 16:26:12

标签: python-3.x encryption int ascii public-key-encryption

我需要将int中的Offset添加到Code中的每个号码 这就是我如何制作Offset

num1 = (randint(33,126))
num2 = (randint(33,126))
num3 = (randint(33,126))
num4 = (randint(33,126))
num5 = (randint(33,126))
num6 = (randint(33,126))
num7 = (randint(33,126))
num8 = (randint(33,126))
key1 = chr(num1)
key2 = chr(num2)
key3 = chr(num3)
key4 = chr(num4)
key5 = chr(num5)
key6 = chr(num6)
key7 = chr(num7)
key8 = chr(num8)
characterkey = (key1 + key2 + key3 + key4 + key5 + key6 + key7 + key8)
Label(window2, text = characterkey, font = font, bg = "Dim grey", fg = "cyan").place(x = 130, y = 300)
offset = int(num1+num2+num3+num4+num5+num6+num7+num8)
offset = int(offset/8)
offset = int(offset - 32)
print(offset)

我已将文本文件转换为ASCII代码

Code =" ".join(str(ord(char))for char in readFile)

但是我需要在代码中为每个转换后的Ascii代码添加偏移量,并且在添加偏移量后如果它等于126以上则减去94

1 个答案:

答案 0 :(得分:0)

我不完全确定你想做什么,但这是我的猜测:

如果您希望能够快速了解​​它的作用:

Code = " ".join(map(str, [char + offset - 94 if char + offset > 126 else char + offset for i in map(ord, readFile)]))

或者当你关心速度时(因为char + offset在上面的例子中计算了两次):

Code = " ".join(map(str, [(lambda x: x if x <= 126 else x - 94)(char + Offset)) for char in map(ord, readFile)]))