简体   繁体   English

如何比较两个列表中的项目并根据结果创建一个新列表?

[英]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)

Output输出

[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 ,当我认为你有倒退时(即附加outputresult )。

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.

相关问题 比较两个不同大小的列表列表并创建一个新列表 - Compare Two different size list of lists and create a new list 比较两个列表的元素 0; 使用 z[0] 中不在 x 中的项目创建新列表列表 - Compare element 0 of two lists of lists; make new list of lists with items in z[0] that are not in x 比较两个列表并创建一个列表 - Compare Two Lists and Create a List 如何比较两个列表与一个列表中的重复项? - How to compare two lists with duplicated items in one list? 如何比较两个具有相似元素的不同列表并使用循环创建没有相似元素的新列表 - How to compare two different lists with similar elements and create a new list without the similar elements using loops 通过乘法从两个列表中创建新列表。 Python - Create new list out of two lists via multiplication. Python 如何匹配两个不同列表中的项目并根据 python 中的匹配项创建新列表? - How to match items in two different lists and create a new list based on the matches in python? 如何将两列都与字符串列表进行比较,以及如何创建包含唯一项的新列? - How to compare two columns both with list of strings and create a new column with unique items? 如何比较 python 中的 N 个列表并创建具有唯一元素的新列表 - How to compare N lists in python and create a new list with unique elements 如何筛选出两个巨大列表的列表项? - How to filter out list items of two huge lists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM