我的任务是使部门和部门列表,教授和学科作为参数接收。我需要过滤整个列表并返回该部门的代码,教授和学科,而无需迭代或使用递归。我已经过滤了该部门的输入,但是我不知道如何列出仅由名字和姓作为第一字典级联的键“ profesor”的新字典。我知道如何通过迭代来实现,但是在任务中,严格规定必须使用map和filter函数来完成,而不能进行迭代或递归。
这是输入列表:
podaci1 = {'odjeli': {'informatika': 'I901', 'matematika': 'M505', 'biologija': 'B020'},
'profesori': [{'ime': 'Pero', 'prezime': 'Peric', 'odjel': 'I901'},
{'ime': 'Ivo', 'prezime': 'Ivic', 'odjel': 'B020'},
{'ime': 'Mara', 'prezime': 'Maric', 'odjel': 'I901'}],
'predmeti': [{'odjel': 'I901', 'popis': ['matematika', 'programiranje', 'web aplikacije']},
{'odjel': 'M505', 'popis': ['analiza', 'statistika', 'fizika', 'algebra']},
{'odjel': 'B020', 'popis': ['biologija', 'kemija', 'matematika']}]}
这是调用我需要获取的函数和输出的示例:
>>>nastava('informatika', podaci1)
('I901', [{'profesor': 'Pero Peric'}, {'profesor': 'Mara Maric'}], [{'predmeti':['matematika', 'programiranje', 'web aplikacije']}])
这是我目前拥有的:
def nastava(odjel,podaci):
brodjela = podaci['odjeli'][odjel]
profesori = podaci['profesori']
profesori = list(filter(lambda x: x['odjel'] in brodjela, profesori))
predmeti = podaci['predmeti']
predmeti = list(filter(lambda x: x['odjel'] in brodjela, predmeti))
popis = predmeti[0]['popis']
rezultat = []
rezultat.append(brodjela)
rezultat.append(profesori)
rezultat.append(predmeti)
print(rezultat)
我得到这个输出:
['I901', [{'ime': 'Pero', 'prezime': 'Peric', 'odjel': 'I901'}, {'ime': 'Mara', 'prezime': 'Maric', 'odjel': 'I901'}], [{'popis': ['matematika', 'programiranje', 'web aplikacije'], 'odjel': 'I901'}]]
解决方案
您还应该使用map,以便可以解析所获得的输出并构建要求的输出方式。Filter在这种情况下还不够。
建立
data = {'odjeli': {'informatika': 'I901', 'matematika': 'M505', 'biologija': 'B020'},
'profesori': [{'ime': 'Pero', 'prezime': 'Peric', 'odjel': 'I901'},
{'ime': 'Ivo', 'prezime': 'Ivic', 'odjel': 'B020'},
{'ime': 'Mara', 'prezime': 'Maric', 'odjel': 'I901'}],
'predmeti': [{'odjel': 'I901', 'popis': ['matematika', 'programiranje', 'web aplikacije']},
{'odjel': 'M505', 'popis': ['analiza', 'statistika', 'fizika', 'algebra']},
{'odjel': 'B020', 'popis': ['biologija', 'kemija', 'matematika']}]}
功能
def nastava(department, data):
code = data['odjeli'][department]
professors = data['profesori']
filtered_professors = filter(lambda z: z['odjel']==code, professors)
predmeti = data['predmeti']
filtered_predmeti = filter(lambda z: z['odjel'] == code, predmeti)
maped_professors = map(lambda z: {'professor': z['ime'] +" "+ z['prezime']}, filtered_professors)
maped_predmeti = map(lambda z: {'predmeti': z['popis']}, filtered_predmeti)
return code, maped_professors, maped_predmeti
Map函数正是这样做的(根据pythonhelp)
帮助内置模块内置功能图:
map(...)map(function,sequence [,sequence,...])->列表
Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).
因此,您必须通过创建执行此操作的函数(在这种情况下,lambda易于阅读和使用)来构建所需的字典。看一下代码和map函数描述。理解起来应该很简单。
python将一个字典的内容添加到另一个字典_将两个字典值合并为一个 然后将其添加到Python中的另一个字典中...