Reputation: 559
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. 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.
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'}]]
Upvotes: 0
Views: 88
Reputation: 59274
You should use map
as well, so that you can parse the output you've got and build the way it is asked. Filter
won't be enough in this case.
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 function does exactly that (According to python help
)
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).
So, you have to build the dictionary demanded by creating functions (in this case, lambda are easier to read and use) that do that.
Take a look at the code and the description of map
function. It should be pretty straight-forward to understand.
Upvotes: 1