逐个检查列表中的每个值是否与另一个列表相同。 (Python 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]
如我们所见,两个列表中有 2 个相同的值。我目前正在尝试找出一个代码来检查列表中的每个值,如果它们按顺序相同,它将 return False 或 True。 (检查每个列表的第一个值是否相同。在这种情况下,他们首先检查 12 和 6 是否相同,并且由于它们不相同,所以它会 return False。他们检查下一个,如果 3 和 3 相同,并且由于它们相同,它们将 return 为真。)
如果有人愿意帮助我就好了。
您可以使用 all
+ zip()
然后你可以将它们与 ==
.
进行比较
来自docs:
all(iterable)
:
Return True if all elements of the iterable are true (or if the iterable is empty).
zip(*iterables)
;
Make an iterator that aggregates elements from each of the 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
for i in range(0, length): # STEP 2
if list_1[i] != list_2[i]:
print(False)
else:
print(True)
步骤 1
确定列表的长度。这可以通过 len()
函数来完成。 len(list_1)
将 return 的长度 list_1
步骤 2
遍历第一个列表中的每个元素,并将其与第二个列表中的相应元素进行比较。如果它们不同则打印 False
否则打印 True
for i in range(0, length):
if list_1[i] != list_2[i]:
print(False)
else:
print(True)
In this case, they check if 12 and 6 is identical first, and since they're not identical, it'll return False. THey they check the next one, if 3 and 3 is identical, and since it's identical, they'll return 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 的编号)第二项(值为 3)和第五项(值为 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
这是结果:
错误的
真的
错误的
错误的
真
(已编辑)
例如,我们有 2 个列表:
list_1 = [12, 3, 45, 2, 50]
list_2 = [6, 3, 30, 5, 50]
如我们所见,两个列表中有 2 个相同的值。我目前正在尝试找出一个代码来检查列表中的每个值,如果它们按顺序相同,它将 return False 或 True。 (检查每个列表的第一个值是否相同。在这种情况下,他们首先检查 12 和 6 是否相同,并且由于它们不相同,所以它会 return False。他们检查下一个,如果 3 和 3 相同,并且由于它们相同,它们将 return 为真。)
如果有人愿意帮助我就好了。
您可以使用 all
+ zip()
然后你可以将它们与 ==
.
来自docs:
all(iterable)
: Return True if all elements of the iterable are true (or if the iterable is empty).
zip(*iterables)
; Make an iterator that aggregates elements from each of the 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
for i in range(0, length): # STEP 2
if list_1[i] != list_2[i]:
print(False)
else:
print(True)
步骤 1
确定列表的长度。这可以通过 len()
函数来完成。 len(list_1)
将 return 的长度 list_1
步骤 2
遍历第一个列表中的每个元素,并将其与第二个列表中的相应元素进行比较。如果它们不同则打印 False
否则打印 True
for i in range(0, length):
if list_1[i] != list_2[i]:
print(False)
else:
print(True)
In this case, they check if 12 and 6 is identical first, and since they're not identical, it'll return False. THey they check the next one, if 3 and 3 is identical, and since it's identical, they'll return 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 的编号)第二项(值为 3)和第五项(值为 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
这是结果: 错误的 真的 错误的 错误的 真
(已编辑)