获取地图以返回列表匹配的所有T / F值

时间:2018-04-11 14:07:15

标签: python python-3.x list

使用以下语法:

from operator import eq
a = ['a','b', 'c']
b = ['a', 'b']
list(map(eq, a, b))

我得到了:

# [True, True]

如何获得缺少T/F元素的'c'结果:

# [True, True, False]

2 个答案:

答案 0 :(得分:2)

使用starmap中的zip_longestitertoolsfillvalue 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]