使用以下语法:
from operator import eq
a = ['a','b', 'c']
b = ['a', 'b']
list(map(eq, a, b))
我得到了:
# [True, True]
如何获得缺少T/F
元素的'c'
结果:
# [True, True, False]
答案 0 :(得分:2)
使用starmap
中的zip_longest
和itertools
与fillvalue
zip_longest
中的In [34]: from itertools import zip_longest, starmap
In [35]: list(starmap(eq, zip_longest(a, b, fillvalue=None)))
Out[35]: [True, True, False]
填充缺失值位置:
while not os.path.exists("../path/"+filename):
# perform some sort of operation
# add some delay if needed.
答案 1 :(得分:1)
以下是不使用itertools
的版本:
def tmp_func(a,b):
i = 0
m = len(max(a,b))
len_diff = len(a) - len(b)
if(len_diff < 0):
a.extend(abs(len_diff)*[''])
elif (len_diff > 0):
b.extend(abs(len_diff)*[''])
while i < m:
yield (a[i], b[i])
i=i+1
a = ['a','b', 'c']
b = ['a', 'b']
list(map(lambda x: x[0]==x[1], tmp_func(a,b)))
输出:
[True, True, False]