简体   繁体   English

更改列表中的多个值

[英]Changing multiple values in a list

I'm writing an encryption code so I don't know exactly where each character I need to replace will be: I have put a text file into a list and then the list into ascii, But i need to replace 32 with a space. 我正在写一个加密代码,所以我不知道我需要替换的每个字符的确切位置:我已经将文本文件放入列表中,然后将列表放入ascii中,但是我需要用空格替换32。 I get this printed out so far but I need to replace 32 on the second list with " " 到目前为止,我已经把它打印出来了,但是我需要用“”替换第二个列表中的32

Original = ['S', 'o', 'm', 'e', 'w', 'h', 'e', 'r', 'e', ' ', 'i', 'n', ' ', 'l', 'a', ' ', 'M', 'a', 'n', 'c', 'h', 'a', ',', ' ', 'i', 'n', ' ', 'a', ' ', 'p', 'l', 'a', 'c', 'e', ' ', 'w', 'h', 'o', 's', 'e', ' ', 'n', 'a', 'm', 'e', ' ', 'I', ' ', 'd', 'o', ' ', 'n', 'o', 't', ' ', 'c', 'a', 'r', 'e', ' ', 't', 'o', ' ', 'r', 'e', 'm', 'e', 'm', 'b', 'e', 'r', ',', ' ', 'a', ' ', 'g', 'e', 'n', 't', 'l', 'e', 'm', 'a', 'n', ' ', 'l', 'i', 'v', 'e', 'd', ' ', 'n', 'o', 't', ' ', 'l', 'o', 'n', 'g', ' ', 'a', 'g', 'o', ',', ' ', 'o', 'n', 'e', ' ', 'o', 'f', ' ', 't', 'h', 'o', 's', 'e', ' ', 'w', 'h', 'o', ' ', 'h', 'a', 's', ' ', 'a', ' ', 'l', 'a', 'n', 'c', 'e', ' ', 'a', 'n', 'd', ' ', 'a', 'n', 'c', 'i', 'e', 'n', 't', ' ', 's', 'h', 'i', 'e', 'l', 'd', ' ', 'o', 'n', ' ', 'a', ' ', 's', 'h', 'e', 'l', 'f', ' ', 'a', 'n', 'd', ' ', 'k', 'e', 'e', 'p', 's', ' ', 'a', ' ', 's', 'k', 'i', 'n', 'n', 'y', ' ', 'n', 'a', 'g', ' ', 'a', 'n', 'd', ' ', 'a', ' ', 'g', 'r', 'e', 'y', 'h', 'o', 'u', 'n', 'd', ' ', 'f', 'o', 'r', ' ', 'r', 'a', 'c', 'i', 'n', 'g', '.']


ASCII_conversion = [83, 111, 109, 101, 119, 104, 101, 114, 101, 32, 105, 110, 32, 108, 97, 32, 77, 97, 110, 99, 104, 97, 44, 32, 105, 110, 32, 97, 32, 112, 108, 97, 99, 101, 32, 119, 104, 111, 115, 101, 32, 110, 97, 109, 101, 32, 73, 32, 100, 111, 32, 110, 111, 116, 32, 99, 97, 114, 101, 32, 116, 111, 32, 114, 101, 109, 101, 109, 98, 101, 114, 44, 32, 97, 32, 103, 101, 110, 116, 108, 101, 109, 97, 110, 32, 108, 105, 118, 101, 100, 32, 110, 111, 116, 32, 108, 111, 110, 103, 32, 97, 103, 111, 44, 32, 111, 110, 101, 32, 111, 102, 32, 116, 104, 111, 115, 101, 32, 119, 104, 111, 32, 104, 97, 115, 32, 97, 32, 108, 97, 110, 99, 101, 32, 97, 110, 100, 32, 97, 110, 99, 105, 101, 110, 116, 32, 115, 104, 105, 101, 108, 100, 32, 111, 110, 32, 97, 32, 115, 104, 101, 108, 102, 32, 97, 110, 100, 32, 107, 101, 101, 112, 115, 32, 97, 32, 115, 107, 105, 110, 110, 121, 32, 110, 97, 103, 32, 97, 110, 100, 32, 97, 32, 103, 114, 101, 121, 104, 111, 117, 110, 100, 32, 102, 111, 114, 32, 114, 97, 99, 105, 110, 103, 46]

any help? 有什么帮助吗?

A simple list comprehension with TrueValue if condition else FalseValue will do: TrueValue if condition else FalseValue则可以使用TrueValue if condition else FalseValue简单的列表理解:

ASCII_conversion = [' ' if x == 32 else x for x in ASCII_conversion]

or combining the two operations 或合并两个操作

ASCII_conversion = [' ' if x == ' ' else ord(x) for x in Original]

Note #1: The above piece of code will produce a list containing both strings and integers. 注意#1:上面的代码段将产生一个包含字符串和整数的列表。 This might not be a good approach. 这可能不是一个好方法。 Try to have all elements of a list of the same type. 尝试使所有元素具有相同类型的列表。 It helps reduce errors. 它有助于减少错误。

Note #2: Please work on on your variable naming convention. 注意#2:请按照您的变量命名约定进行操作。 It sort of hurts readers' eyes. 这有点伤读者的眼睛。

在当前的ASCII_conversion列表之后添加新的代码行:

ASCII_conversion = [x if x!=32 else " " for x in ASCII_conversion]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM