简体   繁体   English

将两个字典值合并为一个,然后将其添加到Python中的另一个字典中

[英]Merge two dictionary values to one and add it to another dictionary in Python

I have a task to make function which receives department and list of departments, professors and subjects as arguments. 我有一个使接收部门和部门列表,教授和学科作为参数的功能。 I need to filter whole list and return department code, professors and subject from that department WITHOUT iterating or using recursions. 我需要过滤整个列表并返回该部门的代码,教授和学科,而无需迭代或使用递归。 I have already filtered the input for that department but I don't know how to make list of newly made dictionaries which contains only name and last name as key "profesor" concatenated from first dictionary. 我已经过滤了该部门的输入,但是我不知道如何制作仅包含名称和姓氏作为从第一个字典连接的键“ profesor”的新字典的列表。 I know how to make it with iteration but in task, it's strictly specified that it must be done with map and filter functions without iteration or recursions. 我知道如何通过迭代来做到这一点,但是在任务中,严格规定必须使用map和filter函数来完成它,而无需迭代或递归。

Here is input list: 这是输入列表:

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']}]}

Here is an example of calling the function and output that I need to get: 这是调用我需要获取的函数和输出的示例:

>>>nastava('informatika', podaci1)

('I901', [{'profesor': 'Pero Peric'}, {'profesor': 'Mara Maric'}], [{'predmeti':['matematika', 'programiranje', 'web aplikacije']}])

This is what I currently have: 这是我目前拥有的:

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)

And I get this for output: 我得到这个输出:

['I901', [{'ime': 'Pero', 'prezime': 'Peric', 'odjel': 'I901'}, {'ime': 'Mara', 'prezime': 'Maric', 'odjel': 'I901'}], [{'popis': ['matematika', 'programiranje', 'web aplikacije'], 'odjel': 'I901'}]]

You should use map as well, so that you can parse the output you've got and build the way it is asked. 您还应该使用map ,以便您可以解析所获得的输出并构建要求的输出方式。 Filter won't be enough in this case. 在这种情况下, Filter是不够的。

Setup 设定

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']}]}

Function 功能

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 function does exactly that (According to python help ) Map函数可以做到这一点(根据python help

Help on built-in function map in module builtin : 帮助内置模块内置功能图:

map(...) map(function, sequence[, sequence, ...]) -> list 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). 

So, you have to build the dictionary demanded by creating functions (in this case, lambda are easier to read and use) that do that. 因此,您必须通过创建执行此操作的函数(在这种情况下,lambda易于阅读和使用)来构建所需的字典。 Take a look at the code and the description of map function. 看一下map功能的代码和说明。 It should be pretty straight-forward to understand. 理解起来应该很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM