Python 使用 for 循环将列表转换为 AscII
Python using for loop to turn a list into AscII
所以我已经为此做了一些工作,但我现在还想不通,我需要使用一个 for 循环函数将数字列表转换为字符串。不完全确定我做错了什么。任何帮助,将不胜感激。
这是列表
[67, 111, 110, 103, 114, 97, 116, 115, 32, 121, 111, 117, 32, 104, 97, 118, 101, 32, 117, 110,
112, 97, 99, 107, 101, 100, 32, 116, 104, 101, 32, 109, 101, 115, 115, 97, 103, 101, 32, 117,
115, 105, 110, 103, 32, 97, 32, 108, 111, 111, 112, 33]
看起来是这样,但在一行中,而不是像“恭喜你已经打开包装
使用循环的消息:“
https://i.stack.imgur.com/9ee8k.png
应该是:
def convert2string():
for i in list1:
print(chr(i), end="")
convert2string()
Congrats you have unpacked the message using a loop!
这是解决您的问题的示例代码片段。您可以通过添加更多标点符号和白色 space 字符来更新它。希望对你有帮助。
data = [67, 111, 110, 103, 114, 97, 116, 115, 32, 121, 111, 117, 32, 104, 97, 118, 101, 32, 117, 110, 112, 97, 99, 107, 101, 100, 32, 116, 104, 101, 32, 109, 101, 115, 115, 97, 103, 101, 32, 117, 115, 105, 110, 103, 32, 97, 32, 108, 111, 111, 112, 33]
white_spaces=[9,10,13,32]
for i in data:
if (i >=65 and i <=90)or (i>=97 and i<=122) or (i in white_spaces):
print("%c"%(i), end='')
所以我已经为此做了一些工作,但我现在还想不通,我需要使用一个 for 循环函数将数字列表转换为字符串。不完全确定我做错了什么。任何帮助,将不胜感激。 这是列表
[67, 111, 110, 103, 114, 97, 116, 115, 32, 121, 111, 117, 32, 104, 97, 118, 101, 32, 117, 110, 112, 97, 99, 107, 101, 100, 32, 116, 104, 101, 32, 109, 101, 115, 115, 97, 103, 101, 32, 117, 115, 105, 110, 103, 32, 97, 32, 108, 111, 111, 112, 33]
看起来是这样,但在一行中,而不是像“恭喜你已经打开包装 使用循环的消息:“ https://i.stack.imgur.com/9ee8k.png
应该是:
def convert2string():
for i in list1:
print(chr(i), end="")
convert2string()
Congrats you have unpacked the message using a loop!
这是解决您的问题的示例代码片段。您可以通过添加更多标点符号和白色 space 字符来更新它。希望对你有帮助。
data = [67, 111, 110, 103, 114, 97, 116, 115, 32, 121, 111, 117, 32, 104, 97, 118, 101, 32, 117, 110, 112, 97, 99, 107, 101, 100, 32, 116, 104, 101, 32, 109, 101, 115, 115, 97, 103, 101, 32, 117, 115, 105, 110, 103, 32, 97, 32, 108, 111, 111, 112, 33]
white_spaces=[9,10,13,32]
for i in data:
if (i >=65 and i <=90)or (i>=97 and i<=122) or (i in white_spaces):
print("%c"%(i), end='')