以下是一个示例代码,演示如何保留满足两个条件的群组:
# 创建一个示例列表,其中包含多个群组
groups = [
{'name': 'Group 1', 'members': 10, 'active': True},
{'name': 'Group 2', 'members': 5, 'active': False},
{'name': 'Group 3', 'members': 15, 'active': True},
{'name': 'Group 4', 'members': 8, 'active': True},
{'name': 'Group 5', 'members': 12, 'active': False}
]
# 定义两个条件函数
def has_enough_members(group):
return group['members'] >= 10
def is_active(group):
return group['active']
# 使用条件函数过滤群组
filtered_groups = filter(lambda group: has_enough_members(group) and is_active(group), groups)
# 打印满足条件的群组
for group in filtered_groups:
print(group['name'])
这段代码首先定义了两个条件函数has_enough_members
和is_active
。然后,通过使用filter()
函数和lambda
表达式,将满足这两个条件的群组筛选出来。最后,使用一个循环打印满足条件的群组的名称。
请注意,这只是一个示例代码,你可以根据自己的需求修改条件函数和群组列表。
下一篇:保留满足另一列条件的行。