自由组合嵌套用Python 写 公司要发礼物 条件是 必须大于等于18岁小于30岁的成年人 同时入职时间需满足大于两年 或者级别大于3才可领奖
时间: 2024-10-15 13:24:18 浏览: 22
在Python中,你可以使用条件表达式(ternary operator)结合列表推导来模拟这个场景。假设我们有员工数据存储在一个字典列表(每个字典代表一个员工,包含年龄、入职时间和级别字段),可以编写如下函数来筛选符合条件的员工:
```python
def eligible_gifts(employees):
# 假设字典结构如下:
# {'age': int, 'years_of_service': int, 'level': int}
eligible_employees = [
{
"name": employee['name'], # 添加员工姓名或其他相关信息
"eligibility": (
employee['age'] >= 18 and employee['age'] < 30 and
(employee['years_of_service'] > 2 or employee['level'] > 3)
)
}
for employee in employees
]
return eligible_employees
# 示例员工数据
employees_data = [
{"name": "Alice", "age": 25, "years_of_service": 4, "level": 5},
{"name": "Bob", "age": 17, "years_of_service": 3, "level": 3},
{"name": "Charlie", "age": 28, "years_of_service": 2, "level": 6},
]
eligible_people = eligible_gifts(employees_data)
print("符合条件的员工:")
for employee in eligible_people:
if employee["eligibility"]:
print(f"{employee['name']} 可以领取礼物")
#
阅读全文