Python Forum
Convert all actions through functions, fill the dictionary from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert all actions through functions, fill the dictionary from a file
#1
I can't figure out how to make functions for the dictionary in the dictionary, how to pass arguments if there is a string. It would be nice if someone could write at least one function, then I would understand the progress of the task and I think I could finish the rest. Thanks in advance!
Here is the code for which I need to do all these tasks:

my_dict={'1':{'surname':'Musk', 'name':'Elon', 'gender': 'm','growth':183},
'2':{'surname':'Holmes', 'name':'Enola', 'gender': 'f','growth':175},
'3':{'surname':'Brown', 'name':'Lilly', 'gender': 'f','growth':178},
'4':{'surname':'Lothbrok', 'name':'Ragnar', 'gender': 'm','growth':189},
'5':{'surname':'Smith', 'name':'Adreana', 'gender': 'f','growth':173},
'6':{'surname':'Moon', 'name':'Larry', 'gender': 'm','growth':184}}
for key in my_dict:
if my_dict[key]['gender']=='m':
male_max_growth = max(my_dict.keys(), key=(lambda key: my_dict[key]['growth']))
tallest_guy=my_dict[male_max_growth]['surname']
print('The tallest guy: ', tallest_guy)

for key in my_dict:
if my_dict[key]['gender']=='f':
female_min_growth = min(my_dict.keys(), key=(lambda key: my_dict[key]['growth']))
lowest_girl=my_dict[female_min_growth]['surname']
print('The lowest girl: ', lowest_girl)

for key in my_dict:
sorted_key=sorted(my_dict.keys(), key=(lambda key: my_dict[key]['surname']))
for i in sorted_key:
student1=sorted_key[0]
student2=sorted_key[1]
student3=sorted_key[2]
student4=sorted_key[3]
student5=sorted_key[4]
student6=sorted_key[5]
print('Information about sorted students: ')
print(my_dict[student1])
print(my_dict[student2])
print(my_dict[student3])
print(my_dict[student4])
print(my_dict[student5])
print(my_dict[student6])
print('The correct order: ')
print(sorted_key)
Reply


Messages In This Thread
Convert all actions through functions, fill the dictionary from a file - by Astone - Oct-25-2020, 11:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 1,298 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  How to convert .trc/.txt file into excel using python ebola 3 2,134 Jan-15-2023, 10:37 PM
Last Post: Yoriz
  Using dictionary to find the most sent emails from a file siliusu 6 7,728 Apr-22-2021, 06:07 PM
Last Post: siliusu
  Updating dictionary in another py file tommy_voet 1 5,027 Mar-28-2021, 07:25 PM
Last Post: buran
  Making a dictionary from a file instyabam 0 1,556 Oct-27-2020, 11:59 AM
Last Post: instyabam
  how can i create a dictionary of dictionaries from a file Astone 2 2,342 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,121 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  how to put text file in a dictionary infected400 2 3,047 Jan-06-2019, 04:43 PM
Last Post: micseydel
  Python code to convert executable file to doc or pdf Micmilli2 3 3,061 Oct-11-2018, 10:15 AM
Last Post: Larz60+
  Dictionary to .txt or .csv file stanthaman42 9 4,817 Aug-08-2018, 03:37 PM
Last Post: Vysero

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020