[英]How to compare items in two lists and create a new list out of results?
As an exercise, I'm trying to code a simple XOR decipher.作为练习,我正在尝试编写一个简单的 XOR 解密。
I want to take a binary input of ASCII and store it in a list.我想采用 ASCII 的二进制输入并将其存储在列表中。 Then take a binary key and also store it in a list.
然后取一个二进制密钥并将其存储在列表中。
A function should compare items from both lists respectively and check whether two items that are being compared are a 0 or 1. If both are 0 or both are 1 then output item = 0. If one item is 0 and the other 1 or vice versa, then output item = 1.函数应该分别比较两个列表中的项目,并检查正在比较的两个项目是 0 还是 1。如果都是 0 或都是 1,则输出 item = 0。如果一个项目是 0,另一个是 1,反之亦然,则输出项 = 1。
For each comparison that the function does, it should store the output binary item in a result
list.对于函数执行的每次比较,它应该将输出二进制项存储在
result
列表中。 Once the function iterates over each item in both lists, it should print the resulting binary output of result
.一旦函数迭代了两个列表中的每个项目,它应该打印
result
的结果二进制输出。
My logic:我的逻辑:
def xor():
binary_plaintext = [1,0,1,0,1,0,0,1, 0,1,0,0,0,1,1,0, 1,1,1,0,0,0,0,1]
binary_key = [1,1,1,1,1,0,1,1, 0,0,0,0,1,1,1,1, 1,0,1,1,0,1,0,1]
result = []
for plaintext_item in binary_plaintext and key_item in binary_key:
if plaintext_item == 1 and key_item == 1: # XOR 1|1 = 0
output = 0
output.append(result)
elif plaintext_item == 0 and key_item == 0: # XOR 0|0 = 0
output = 0
output.append(result)
elif plaintext_item == 1 and key_item == 0: # XOR 1|0 = 1
output = 1
output.append(result)
elif plaintext_item == 0 and key_item == 1: # XOR 0|1 = 1
output = 1
output.append(result)
print(result)
xor()
My example doesn't work.我的例子不起作用。 What would be the correct way of comparing binary_plaintext list and binary_key list and iterating their items so that an output value can be stored in a new list?
比较 binary_plaintext 列表和 binary_key 列表并迭代它们的项目以便输出值可以存储在新列表中的正确方法是什么?
Basically you can implement an XOR with the addition of both inputs and then modulo 2.基本上,您可以通过添加两个输入然后模 2 来实现 XOR。
For example that would work:例如,这将工作:
result = [(x+y) % 2 for x,y in zip(binary_plaintext,binary_key)]
print(result)
[0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0]
你永远不会改变result
,尽管你反复将它附加到output
,当我认为你有倒退时(即附加output
到result
)。
This should help:这应该有帮助:
result = [int(binary_plaintext[entry]!=binary_key[entry]) for entry in range(len(binary_plaintext))]
# [0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0]
A couple of mistakes in your function - firstly, to iterate over the lists in parallel, you can use zip()
- so we can replace您的函数中有几个错误 - 首先,要并行迭代列表,您可以使用
zip()
- 所以我们可以替换
for plaintext_item in binary_plaintext and key_item in binary_key:
with和
for plaintext_item, key_item in zip(binary_plaintext, binary_key):
Secondly, you need to use其次,你需要使用
result.append(output)
instead of代替
output.append(result)
And lastly, unindent print(result)
to take it out of the for loop.最后,取消缩进
print(result)
将其从 for 循环中取出。
The corrected function then prints修正后的函数然后打印
[0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0]
For reference you can do this with numpy.作为参考,您可以使用 numpy 执行此操作。
import numpy as np
x = np.asarray([0,0,1,1])
y = np.asarray([0,1,0,1])
xor = x ^ y
In your own implementation, use zip
to get matching pairs from two lists.在您自己的实现中,使用
zip
从两个列表中获取匹配对。 You've also interchanged output and results by mistake:您还错误地交换了输出和结果:
def xor():
binary_plaintext = [1,0,1,0,1,0,0,1, 0,1,0,0,0,1,1,0, 1,1,1,0,0,0,0,1]
binary_key = [1,1,1,1,1,0,1,1, 0,0,0,0,1,1,1,1, 1,0,1,1,0,1,0,1]
result = []
for plaintext_item, key_item in zip(binary_plaintext, binary_key):
if plaintext_item == 1 and key_item == 1: # XOR 1|1 = 0
output = 0
result.append(output)
elif plaintext_item == 0 and key_item == 0: # XOR 0|0 = 0
output = 0
result.append(output)
elif plaintext_item == 1 and key_item == 0: # XOR 1|0 = 1
output = 1
result.append(output)
elif plaintext_item == 0 and key_item == 1: # XOR 0|1 = 1
output = 1
result.append(output)
print(result)
xor()
We can use the python ^ operator to do this quite simply:我们可以使用 python ^ 运算符来非常简单地做到这一点:
def xor():
binary_plaintext = [1,0,1,0,1,0,0,1, 0,1,0,0,0,1,1,0, 1,1,1,0,0,0,0,1]
binary_key = [1,1,1,1,1,0,1,1, 0,0,0,0,1,1,1,1, 1,0,1,1,0,1,0,1]
return [a ^ b for (a, b) in zip(binary_plaintext, binary_key]
We zip together binary_plaintext and binary_key and XOR the contents of each tuple in a list comprehension.我们将 binary_plaintext 和 binary_key 压缩在一起,并对列表理解中每个元组的内容进行异或。
This assumes binary_plaintext and binary_key are the same length.这假设 binary_plaintext 和 binary_key 的长度相同。
You can use zip.您可以使用压缩包。 I'm using python3.
我正在使用python3。
for text,key in zip(binary_plaintext, binary_key)
result.append(text ^ key)
this should work, edited your code for correct result这应该有效,编辑您的代码以获得正确的结果
def xor():
binary_plaintext = [1,0,1,0,1,0,0,1, 0,1,0,0,0,1,1,0, 1,1,1,0,0,0,0,1]
binary_key = [1,1,1,1,1,0,1,1, 0,0,0,0,1,1,1,1, 1,0,1,1,0,1,0,1]
result = []
for i in range(0,len(binary_plaintext)):
if binary_plaintext[i] == binary_key[i]: # XOR 1|1 = 0 # XOR 0|0 = 0
output = 0
result.append(output)
elif binary_plaintext[i] == 1 and binary_key[i] == 0: # XOR 1|0 = 1
output = 1
result.append(output)
elif binary_plaintext[i] == 0 and binary_key[i] == 1: # XOR 0|1 = 1
output = 1
result.append(output)
print(result)
xor()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.