Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b514de2

Browse files
Add files via upload
1 parent 66d7598 commit b514de2

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

‎example.json‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
{
3+
"first name": "Greg",
4+
"last name": "Dean",
5+
"certificate": true,
6+
"scores": [
7+
70,
8+
80,
9+
90
10+
],
11+
"description": "Good job, Greg"
12+
},
13+
{
14+
"first name": "Wirt",
15+
"last name": "Wood",
16+
"certificate": true,
17+
"scores": [
18+
70,
19+
80,
20+
90
21+
],
22+
"description": "Nicely Done"
23+
}
24+
]

‎json_format.py‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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])

‎students.json‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
{
3+
"certificate": true,
4+
"description": "Good job, Greg",
5+
"first_name": "Greg",
6+
"last_name": "Dean",
7+
"scores": [
8+
70,
9+
80,
10+
90
11+
]
12+
},
13+
{
14+
"certificate": true,
15+
"description": "Nicely Done",
16+
"first_name": "Wirt",
17+
"last_name": "Wood",
18+
"scores": [
19+
80,
20+
80.2,
21+
80
22+
]
23+
}
24+
]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /