繁体   English   中英

一一检查列表中的每个值是否与另一个列表相同。 (蟒蛇 3)

[英]Checking if each values in list are identical to another list one by one. (Python 3)

例如,我们有 2 个列表:

list_1 = [12, 3, 45, 2, 50]
list_2 = [6, 3, 30, 5, 50]

正如我们所看到的,我们在两个列表中有两个相同的值。 我目前正在尝试找出一个代码,如果两个值按顺序相同,它将检查列表中的每个值,并且它将返回 False 或 True。 (检查每个列表的第一个值是否相同。在这种情况下,他们首先检查 12 和 6 是否相同,由于它们不相同,它将返回 False。他们检查下一个,如果 3 和3 是相同的,并且由于它是相同的,它们将返回 True。)

如果有人愿意帮助我就好了。

只需将它们与==运算符进行比较。

您也可以将它们与map()all()

all(map(lambda x, y: x == y, list_1, list_2))

试试这个:

list_1 = [12, 3, 45, 2, 50]
list_2 = [6, 3, 30, 5, 50]

list_3 = [1, 2, 3, 4]
list_4 = [1, 2, 3, 4]

print(all(map(lambda x, y: x == y, list_1, list_2)))
print(all(map(lambda x, y: x == y, list_3, list_4)))

输出:

False
True

您也可以手动比较元素:

def compare_lists(list_1, list_2):
    if(len(list_1) != len(list_2)):
        return False

    for i in range(len(list_1)):
        if list_1[i] != list_2[i]:
            return False

    return True

您可以使用all + zip()然后您可以将它们与==进行比较。

文档

all(iterable) :如果可迭代对象的所有元素都为真(或者可迭代对象为空),则返回 True。

zip(*iterables) ; 制作一个迭代器,聚合来自每个可迭代对象的元素。

list_1 = [12, 3, 45, 2, 50]
list_2 = [6, 3, 30, 5, 50]

def check_lists(l1,l2):
    return all(x==y for x,y in zip(l1,l2))

print (check_lists(list_1,list_2))

或者,您可以直接比较它们

print(list_1==list_2)

我们可以使用 for 循环来实现这一点。 (我知道这种方法是老派,您可以通过使用 map() 和 all() 函数使用单线实现相同的效果)

我先写完整的代码。 然后我会解释每个步骤。

(我假设列表将具有相同数量的元素。)

代码:-

list_1 = [12, 3, 45, 2, 50]
list_2 = [12, 3, 45, 2, 50]

length = len(list_1)                    # STEP 1

result = True                           # STEP 2

for i in range(0, length):              # STEP 3
    if list_1[i] != list_2[i]:
        result = False
        break

print(result)                           # STEP 4

第1步

确定列表的长度。 这可以通过len()函数来完成。 len(list_1)将返回 list_1 的长度

第2步

声明一个变量result来存储结果。 将其初始化为True 通过这样做,我们假设我们的列表是相同的。 如果它们不是那么我们将result的值更改为False

result = True

第 3 步

遍历第一个列表中的每个元素,并将其与第二个列表中的相应元素进行比较。 如果它们不同,则将result的值更改为False并跳出for循环,因为无需检查其他元素。

for i in range(0, length):
    if list_1[i] != list_2[i]:
        result = False
        break

第四步

打印出最终输出print(result)

在这种情况下,他们首先检查 12 和 6 是否相同,并且由于它们不相同,因此将返回 False。 他们检查下一个,如果 3 和 3 相同,并且由于相同,他们将返回 True。

您可以使用itertools.zip_longest来迭代多个序列,并一次拥有它们的第 n 个项目,例如,在您的列表中将是 12 和 6,然后是 3 和 3,然后是 45 和 30,依此类推。

  • 这也将处理列表具有不同长度的情况。
from itertools import zip_longest

list_1 = [12, 3, 45, 2, 50]
list_2 = [6, 3, 30, 5, 50]

result = [
    value_1 == value_2
    for value_1, value_2 in zip_longest(list_1, list_2)
]
print(result)

输出:

[False, True, False, False, True]

在这里,您可以看到(基于 1 的编号)第 2 个(值为 3)和第 5 个(值为 50)项是相同的,因此只有True

一种手动检查和比较列表中每个值的手动方法。 list_1 = [12, 3, 45, 2, 50] list_2 = [6, 3, 30, 5, 50]

if(len(list_1) == len(list_1)):
    for i in range(0,len(list_1)):
        print(list_1[i] == list_2[i])
else:
    print(False) #note that If this line is executed we will get only one output  

这是结果:假真假假真

(已编辑)

暂无
暂无

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

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