python 过滤掉混合列表中的 NoneType

python filter out NoneType in mixed list

从 ldap 搜索结果中获取此列表,尝试解析为 csv。但是必须先过滤掉None的。但得到的内容与 search_res 相同。我做错了什么?

search_res:

[('CN=GON,OU=App,OU=Groups,DC=com', {'member': [b'CN=user1,OU=Users,DC=com', b'CN=user2,OU=Users,DC=com',]}), (None, ['ldap://skogen.com/CN=Sche,CN=Conf,DC=com']), (None, ['ldap://skogen.com/CN=Sche,CN=Conf,DC=com'])]

拉姆达:

lame = lambda x: (x is not None)
list_out = list(filter(lame, search_res))
print(list_out)

我想要的 csv 是:

user1
user2

您的列表条目是元组,您正在检查元组是否是 None。但是,您似乎想要检查元组的第一个元素不是 None。所以你已经改变你的过滤器功能为

lame = lambda x: (x and x[0] is not None)

这现在检查整个 None 个元素和第一个不是 None 的元素。