在Python中将两个字典值合并到一个,并将其添加到另一个字典中

2024-05-21 05:29:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个任务,使功能接收部门和部门,教授和学科作为论点的名单。我需要过滤整个列表,并返回部门代码,教授和学科从该部门没有迭代或使用递归。我已经过滤了该部门的输入,但我不知道如何制作新制作的字典列表,其中只包含名字和姓氏作为键“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'}]]

Tags: 列表filter部门imeprezimerezultatpodaciprofesor
1条回答
网友
1楼 · 发布于 2024-05-21 05:29:56

您还应该使用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

Help on built-in function map in module builtin:

map(...) map(function, sequence[, sequence, ...]) -> list

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函数的代码和描述。理解起来应该很直截了当。你知道吗

相关问题 更多 >