In: Computer Science
Python pls
create a function called search_position. This function returns a dictionary.
team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2, 'Top': 5},'Shaco': {'Jungle': 4, 'Top': 2, 'Mid': 1}}
def search_position(team1):
should return
{'Top': {'Fiora': 1, 'Yasuo':5,'Olaf':3,'Shaco':},
'Jungle': {'Shaco': 4},
'Mid': {'Yasuo', 2, 'Fiora': 4,'Olaf':2},
'Bottom': {'Fiora': 3},
'Support': {'Olaf': 4}}
*******IMPORTANT***************
The result should be alphabetical order.
team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2, 'Top': 5},'Shaco': {'Jungle': 4, 'Top': 2, 'Mid': 1}}
def search_position(team1):
d=dict()
l=[]
for k,v in team1.items():
temp=list(v.keys())#get the positions
for j in temp:
if j not in l:#append to list if position is not present in list
l
l.append(j)#get the unique positions
for i in l:
d[i]=dict()#create dictionary of positions
for k,v in team1.items():
for k1,v1 in v.items():
d[k1][k]=v1#from team1 dictionaey get the values
temp=(sorted(d.items(),key=lambda k:(k[0],k[1])))#sort
alphabetically and store the returned list in temp
d=dict()
for i in temp:#for each tuple in list temp, get the key and value
pairs to dictionary
d[i[0]]=i[1]
return d#return dictionary
print(search_position(team1))
Screenshots:
The screenshots are attached below for reference.
Please follow them for output and proper indentation.