is_it_equal_func_1 = lambda x:x in list1
is_it_equal_list_2 = map(is_it_equal_func_1, list2)
print(list(is_it_equal_list_2))
# prints [True, True, True, True, True]
is_it_equal_func_3 = lambda y:y in list3
is_it_equal_list_4 = map(is_it_equal_func_3, list1)
print(list(is_it_equal_list_4))
# prints [True, True, True, True, True]
如果在列表之一中得到False
,如何比较每个布尔值?
尝试:
is_it_equal_func_5 = lambda y:y in str(list(is_it_equal_list_2))
is_it_equal_func_6 = map(is_it_equal_func_5, list(is_it_equal_list_4))
print(list(is_it_equal_func_6))
# prints [True, True]
# I want [True, True, True, True, True]
答案 0 :(得分:0)
您正在寻找“ zip”:
results = [(v1 == v2) for v1,v2 in zip(list1, list2)]
print(results)
(未经测试)
答案 1 :(得分:0)
使用以下方法比较两个列表并获取结果的布尔列表。即使两个列表的大小不同,以下答案也将起作用。将itertools.zip_longest
用于Python 3.x
import itertools
alist = [1,2,3,4]
blist = [3,2,5]
compare_list = [(a == b) for a,b in itertools.izip_longest(alist, blist)]
print compare_list
输出:
[False, True, False, False]