我对使用lambda过滤列表的理解是,对于lambda函数,过滤器将返回列表中所有返回True的元素。在这种情况下,对于以下代码,
inputlist = []
inputlist.append(["1", "2", "3", "a"])
inputlist.append(["4", "5", "6", "b"])
inputlist.append(["1", "2", "4", "c"])
inputlist.append(["4", "5", "7", "d"])
outputlist = filter(lambda x: (x[0] != "1" and x[1] != "2" and x[2] != "3"), inputlist)
for item in outputlist: print(item)
输出应为
['4', '5', '6', 'b']
['1', '2', '4', 'c']
['4', '5', '7', 'd']
但是我得到的输出是
['4', '5', '6', 'b']
['4', '5', '7', 'd']
如果我使用
outputlist = filter(lambda x: (x[0] != "1" or x[1] != "2" or x[2] != "3"), inputlist)
我在这里做什么傻事?还是我的理解不正确?
转载请注明出处:http://www.hnlovelyyears.com/article/20230526/1084875.html