|
| 1 | +# Working with files in json formats |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +# Creating dictionaries with information about the students |
| 6 | +student1 = { |
| 7 | + 'first_name': 'Greg', |
| 8 | + 'last_name': 'Dean', |
| 9 | + 'scores': [70, 80, 90], |
| 10 | + 'description': 'Good job, Greg', |
| 11 | + 'certificate': True |
| 12 | +} |
| 13 | + |
| 14 | +student2 = { |
| 15 | + 'first_name': 'Wirt', |
| 16 | + 'last_name': 'Wood', |
| 17 | + 'scores': [80, 80.2, 80], |
| 18 | + 'description': 'Nicely Done', |
| 19 | + 'certificate': True |
| 20 | +} |
| 21 | + |
| 22 | +# Creating a list with elements of dictionaries |
| 23 | +data = [student1, student2] |
| 24 | + |
| 25 | +# Showing the results with the help of library json |
| 26 | +print(json.dumps(data, indent=4, sort_keys=True)) |
| 27 | + |
| 28 | +# Writing the data inside the file |
| 29 | +with open('students.json', 'w') as f: |
| 30 | + json.dump(data, f, indent=4, sort_keys=True) |
| 31 | + |
| 32 | +# Loading data from json into the Python object as list with dictionaries |
| 33 | +data_json = json.dumps(data, indent=4, sort_keys=True) |
| 34 | +data_again = json.loads(data_json) |
| 35 | + |
| 36 | +# Calculating the sum of all scores for the first student |
| 37 | +print(sum(data_again[0]["scores"])) |
| 38 | + |
| 39 | +# Loading data from json file |
| 40 | +with open("students.json", "r") as f: |
| 41 | + data_again = json.load(f) |
| 42 | + |
| 43 | +# Calculating the sum of all scores for the second student |
| 44 | +print(sum(data_again[1]["scores"])) |
| 45 | + |
| 46 | + |
| 47 | +# Implementing the task |
| 48 | +# As an input there is a description of classes in json format |
| 49 | +# Calculate the number of inheritance for each class |
| 50 | +# Results has to be represented in alphabetical order |
| 51 | +# Converting input string into json format and then into list of dictionaries |
| 52 | +# data_json = input() |
| 53 | +# data = json.loads(dat) |
| 54 | + |
| 55 | +# Tested data with a list of dictionaries in json format |
| 56 | +data1 = [{"name": "A", "parents": []}, {"name": "B", "parents": ["A", "C"]}, {"name": "C", "parents": ["A"]}] |
| 57 | +data = [{"name": "B", "parents": ["A", "C"]}, {"name": "C", "parents": ["A"]}, {"name": "A", "parents": []}, {"name": "D", "parents":["C", "F"]}, {"name": "E", "parents":["D"]}, {"name": "F", "parents":[]}] |
| 58 | + |
| 59 | +# Creating a dictionary for storing the resulted information |
| 60 | +d = {} |
| 61 | + |
| 62 | +# Creating a list for sorting the future results in alphabetic order |
| 63 | +list_of_names = [] |
| 64 | + |
| 65 | +# Going through all names and writing the all unique names of classes inside new dictionary |
| 66 | +for i in range(len(data)): |
| 67 | + for x, y in data[i].items(): |
| 68 | + if x == "name": |
| 69 | + if y not in d: |
| 70 | + d[y] = 1 |
| 71 | + list_of_names += [y] |
| 72 | + |
| 73 | +# Going through all parents and adding number of met names of classes into appropriate key |
| 74 | +for i in range(len(data)): |
| 75 | + for x, y in data[i].items(): |
| 76 | + if x == "parents": |
| 77 | + for j in range(len(y)): |
| 78 | + if y[j] in d: |
| 79 | + d[y[j]] += 1 |
| 80 | + |
| 81 | +# Sorting list of names |
| 82 | +sorted(list_of_names) |
| 83 | + |
| 84 | +# Showing the result in alphabetic order |
| 85 | +for x in list_of_names: |
| 86 | + print(x, ':', d[x]) |
0 commit comments