Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中添加列表/字符串ord()值_Python - Fatal编程技术网

在Python中添加列表/字符串ord()值

在Python中添加列表/字符串ord()值,python,Python,基本上,我想把两个列表加在一起,列表由字母、字符串组成。 我想把这些列表加在一起,但并不像你想象的那么简单。 我想把一个列表中每个字母的值加在另一个列表中每个字母的值上,如果我要做: ord('a') + ord('b') 不是: 'a' + 'b' 我将在下面复制粘贴我的代码,让每个人都能看到,并在执行代码时显示输出 import itertools from itertools import cycle uinput= input("Enter a keyword: ") word =

基本上,我想把两个列表加在一起,列表由字母、字符串组成。 我想把这些列表加在一起,但并不像你想象的那么简单。 我想把一个列表中每个字母的值加在另一个列表中每个字母的值上,如果我要做:

ord('a') + ord('b')
不是

'a' + 'b'
我将在下面复制粘贴我的代码,让每个人都能看到,并在执行代码时显示输出

import itertools
from itertools import cycle
uinput= input("Enter a keyword: ")
word = cycle(uinput)
uinput2= input("Enter a message to encrypt: ")
message = (uinput2)
rkey =(''.join([next(word) for c in message]))
rkeylist = list(rkey)
mlist= list(message)
print(rkeylist)
print(mlist)

encoded1 = [rkeylist[i]+mlist[i] for i in range(len(rkeylist))]
print(encoded1)
输出:

Enter a keyword: keyword
Enter a message to encrypt: Hello, Hello!
['k', 'e', 'y', 'w', 'o', 'r', 'd', 'k', 'e', 'y', 'w', 'o', 'r']
['H', 'e', 'l', 'l', 'o', ',', ' ', 'H', 'e', 'l', 'l', 'o', '!']
['kH', 'ee', 'yl', 'wl', 'oo', 'r,', 'd ', 'kH', 'ee', 'yl', 'wl', 'oo', 'r!']
>>> 
如您所见,列表已添加在一起,但没有添加值,只有字母,因此我正在执行以下操作:

'a' + 'b'
而不是我想要的代码:

ord('a') + ord('b')

我想你要么在寻找:

>>> uinput=['k', 'e', 'y', 'w', 'o', 'r', 'd', 'k', 'e', 'y', 'w', 'o', 'r']
>>> uinput2=['H', 'e', 'l', 'l', 'o', ',', ' ', 'H', 'e', 'l', 'l', 'o', '!']
>>> [ord(i)+ord(j) for i,j in zip(uinput,uinput2)]
[179, 202, 229, 227, 222, 158, 132, 179, 202, 229, 227, 222, 147]
或:

或:


请注意,
%26+97
将结果映射到有效的字母字符。

+1!只需将uinput2更新到他在帖子中提到的内容,否则将是错误的confusing@user3378649哦我的错误。谢谢你的建议。谢谢你的帮助伙计们:如果你想添加
ord
s,为什么要显式地添加字符串?
>>> [chr((ord(i)+ord(j))%26+97) for i,j in zip(uinput,uinput2)]
['x', 'u', 'v', 't', 'o', 'c', 'c', 'x', 'u', 'v', 't', 'o', 'r']
>>> "".join([chr((ord(i)+ord(j))%26+97) for i,j in zip(uinput,uinput2)])
'xuvtoccxuvtor'