我有一個字典的串列作為資料輸入。我還有一個函式,我想根據 "型別 "鍵迭代并移除單個字典(即移除Pippin),以便在進一步迭代尋找 "未經認證的安裝者 "時,他不會被算作一個潛在選項。
這可能嗎?下面的代碼。
字典的串列
employees=[
{
'name': 'Pippin',
'type':'uncertified installer',
'day_available': ['M','Tu','W', 'Th', 'F']
},
{
'name':'Merry',
'type':'handyperson',
'day_available': ['M','Tu','W','Th','F']
},
{
'name':'Sam',
'type':'handyperson',
'day_available': ['M','Tu','W','Th','F']
},
{
'name': 'Frodo',
'type':'Uncertified Installer',
'day_available': ['M','Tu','W','Th','F']
}
到目前為止的函式
def schedule(buildings, employees)。
for building in buildings:
remaining_employee_list=employees.copy()
if building == 'single':
remaining_employee_list[:]=[d for d in employees if (d.get('type')! ='handyperson') and (d.get('type')!='uncertified installer')]
buildings.remove('single')
print(building, remaining_employee_list)
uj5u.com熱心網友回復:
List comprehensions可能是最簡單和最習慣的方式:
employees = [
{
"name"/span>: "Pippin"。
"type": "未經認證的安裝人員"。
"day_available": ["M"/span>, "Tu"/span>, "W"/span>, "Th"/span>, "F"/span>] 。
},
{
"name": "Merry",
"type": "Handyperson",
"day_available": ["M"/span>, "Tu"/span>, "W"/span>, "Th"/span>, "F"/span>] 。
},
]
認證的員工 = [
雇員
for employee in employees
if employee.get("type") != "未認證安裝人員" # 這里可以添加額外條件(例如對建筑物件的檢查)
]
uj5u.com熱心網友回復:
這樣如何:-
employees=[
{
'name': 'Pippin',
'type':'uncertified installer',
'day_available': ['M','Tu','W', 'Th', 'F']
},
{
'name':'Merry',
'type':'handyperson',
'day_available': ['M','Tu','W','Th','F']
},
{
'name':'Sam',
'type':'handyperson',
'day_available': ['M','Tu','W','Th','F']
},
{
'name': 'Frodo',
'type':'Uncertified Installer',
'day_available': ['M','Tu','W','Th','F']
}]
UI = '未經認證的安裝人員' '未認證的安裝人員
E = [d for d in employees if d.get('type', UI) !=UI]
print(E)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/326658.html
標籤:
上一篇:在js中制作一個檢查質數的函式
