对于字符串中的每个整数,我想在 - Python 上添加一个整数
For every integer in a string I want to add an integer on - Python
我已经将几个句子转换成 ASCII 并删除了所有空格。但是对于字符串中的所有内容,我希望将 20 添加到其中。
所以现在对于打印的每个字符,我需要将 20 个添加到每个单独的字符中。但我不确定该怎么做。
谢谢。
如果您的 ascii 值在一个列表中,而您希望新值在另一个列表中,您可以像这样使用 map
函数:
new_list = map(lambda x : x + 20, old_list)
你可以试试这个,
a = [83, 111, 109, 101, 119, 104, 101, 114, 101, 105, 110, 108, 97, 77, 97, 110, 99, 104, 97, 44, 105, 110, 97, 112, 108, 97, 99, 101, 119, 104, 111, 115, 101, 110, 97, 109, 101, 73, 100, 111, 110, 111, 116, 99, 97, 114, 101, 116, 111, 114, 101, 109, 101, 109, 98, 101, 114, 44, 97, 103, 101, 110, 116, 108, 101, 109, 97, 110, 108, 105, 118, 101, 100, 110, 111, 116, 108, 111, 110, 103, 97, 103, 111, 44, 111, 110, 101, 111, 102, 116, 104, 111, 115, 101, 119, 104, 111, 104, 97, 115, 97, 108, 97, 110, 99, 101, 97, 110, 100, 97, 110, 99, 105, 101, 110, 116, 115, 104, 105, 101, 108, 100, 111, 110, 97, 115, 104, 101, 108, 102, 97, 110, 100, 107, 101, 101, 112, 115, 97, 115, 107, 105, 110, 110, 121, 110, 97, 103, 97, 110, 100, 97, 103, 114, 101, 121, 104, 111, 117, 110, 100, 102, 111, 114, 114, 97, 99, 105, 110, 103, 46]
added_list = []
for item in a:
added_list = added_list + [item+20]
print (added_list)
您似乎试图将所有字符向上移动 20 位来制作一个简单的密码。另一种方法是使用翻译 table:
s_from = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
s_to = 'uvwxyzabcdefghijklmnopqrstUVWXYZABCDEFGHIJKLMNOPQRST'
cypher_table = str.maketrans(s_from, s_to)
print("Enter the name of the file you would like to encrypt, ensuring you type .txt afterwards")
filename = input()
sample = open(filename, 'r').read()
print(sample)
print()
print(sample.translate(cypher_table))
因此,如果您输入的是 Abc
,它会给您 Uvw
。要反转这个过程,你只需要交换 s_from
和 s_to
左右。
我已经将几个句子转换成 ASCII 并删除了所有空格。但是对于字符串中的所有内容,我希望将 20 添加到其中。
所以现在对于打印的每个字符,我需要将 20 个添加到每个单独的字符中。但我不确定该怎么做。
谢谢。
如果您的 ascii 值在一个列表中,而您希望新值在另一个列表中,您可以像这样使用 map
函数:
new_list = map(lambda x : x + 20, old_list)
你可以试试这个,
a = [83, 111, 109, 101, 119, 104, 101, 114, 101, 105, 110, 108, 97, 77, 97, 110, 99, 104, 97, 44, 105, 110, 97, 112, 108, 97, 99, 101, 119, 104, 111, 115, 101, 110, 97, 109, 101, 73, 100, 111, 110, 111, 116, 99, 97, 114, 101, 116, 111, 114, 101, 109, 101, 109, 98, 101, 114, 44, 97, 103, 101, 110, 116, 108, 101, 109, 97, 110, 108, 105, 118, 101, 100, 110, 111, 116, 108, 111, 110, 103, 97, 103, 111, 44, 111, 110, 101, 111, 102, 116, 104, 111, 115, 101, 119, 104, 111, 104, 97, 115, 97, 108, 97, 110, 99, 101, 97, 110, 100, 97, 110, 99, 105, 101, 110, 116, 115, 104, 105, 101, 108, 100, 111, 110, 97, 115, 104, 101, 108, 102, 97, 110, 100, 107, 101, 101, 112, 115, 97, 115, 107, 105, 110, 110, 121, 110, 97, 103, 97, 110, 100, 97, 103, 114, 101, 121, 104, 111, 117, 110, 100, 102, 111, 114, 114, 97, 99, 105, 110, 103, 46]
added_list = []
for item in a:
added_list = added_list + [item+20]
print (added_list)
您似乎试图将所有字符向上移动 20 位来制作一个简单的密码。另一种方法是使用翻译 table:
s_from = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
s_to = 'uvwxyzabcdefghijklmnopqrstUVWXYZABCDEFGHIJKLMNOPQRST'
cypher_table = str.maketrans(s_from, s_to)
print("Enter the name of the file you would like to encrypt, ensuring you type .txt afterwards")
filename = input()
sample = open(filename, 'r').read()
print(sample)
print()
print(sample.translate(cypher_table))
因此,如果您输入的是 Abc
,它会给您 Uvw
。要反转这个过程,你只需要交换 s_from
和 s_to
左右。