Skip to content

Commit d10f0aa

Browse files
authored
Update 3. Get Only What You Need, and Fast.md
1 parent 5863b7a commit d10f0aa

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Introduction to MongoDB in Python/3. Get Only What You Need, and Fast.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,28 @@ full_names = [doc["firstname"] + " " + doc["surname"] for doc in docs]
6161
# Print the full names
6262
print(full_names)
6363
```
64+
## 🦍 Doing our share of data validation
65+
Each "laureates.share" value appears to be the reciprocal of a laureate's fractional share of that prize, encoded as a string. For example, a laureate "share" of "4" means that this laureate received a
66+
67+
share of the prize. Let's check that for each prize, all the shares of all the laureates add up to 1!
68+
- [x] Save a list of prizes (prizes), projecting out only the "laureates.share" values for each prize.
69+
- [x] For each prize, compute the total share as follows:
70+
- [x] Initialize the variable total_share to 0.
71+
- [x] Iterate over the laureates for each prize, converting the "share" field of the "laureate" to float and adding the reciprocal of it (that is, 1 divided by it) to total_share.
72+
```py
73+
# Save documents, projecting out laureates share
74+
prizes = db.prizes.find({}, ["laureates.share"])
75+
76+
# Iterate over prizes
77+
for prize in prizes:
78+
# Initialize total share
79+
total_share = 0
80+
81+
# Iterate over laureates for the prize
82+
for laureate in prize["laureates"]:
83+
# add the share of the laureate to total_share
84+
total_share += 1 / float(laureate['share'])
85+
86+
# Print the total share
87+
print(total_share)
88+
```

0 commit comments

Comments
 (0)