42 — Compare: two groups of numbers
Guide 30 summarized one list of numbers. Comparing two lists asks the same questions twice, then decides which group is bigger.
Steps
Create and load the two data files, each holding one JSON list (the shape guide 14 saved with
json_save):
run it →# a.json [5, 2, 9, 1, 7, 3] # b.json [8, 4, 6, 10, 3]
run it →use file latest a = json_load("a.json") b = json_load("b.json") show f"a.json: {a}" show f"b.json: {b}"It prints
a.json: [5, 2, 9, 1, 7, 3]andb.json: [8, 4, 6, 10, 3].Mean by hand, mean by library, and max.
sum(a) / len(a)is the mean;statistics.mean(guide 30) checks it;maxis a builtin:
run it →from statistics import mean a = [5, 2, 9, 1, 7, 3] a_mean = sum(a) / len(a) show f"a mean: {a_mean}" show f"a mean from statistics: {mean(a)}" show f"a max: {max(a)}"a is 4.5 (27 over 6), with max 9. For b the sum is 31 over 5, so its mean is 6.2 and its max is 10.
The comparison program. Save
compare.nmewith both JSON files, and run it:
run it →# compare.nme — which group of numbers is bigger? # Run: nme r compare # The files a.json and b.json must exist in the same folder. use file latest from statistics import mean a = json_load("a.json") b = json_load("b.json") # Mean by hand, max by builtin. a_mean = sum(a) / len(a) b_mean = sum(b) / len(b) a_max = max(a) b_max = max(b) show f"a: mean {a_mean}, max {a_max}" show f"b: mean {b_mean}, max {b_max}" # statistics.mean checks the hand-written answer. show f"a mean from statistics: {mean(a)}" show f"b mean from statistics: {mean(b)}" # Compare the two groups. if a_mean > b_mean: show "a has the higher mean" else: show "b has the higher mean" if a_max > b_max: show "a has the higher max" else: show "b has the higher max"nme r comparea: mean 4.5, max 9 b: mean 6.2, max 10 a mean from statistics: 4.5 b mean from statistics: 6.2 a has the higher mean b has the higher maxbwins both comparisons. Thestatisticslines confirm the hand-written means. The Korean guide usesjson읽기and보여줘— full pair in 42-compare.ko.md.
Try it yourself
Change b.json to [3, 1, 4, 1, 5, 9] and rerun — the answers flip.
What you learned
json_loadturns each JSON list file into a Python list.sum(numbers) / len(numbers)is the mean written by hand.statistics.meangives the same answer in one call.max(numbers)is a builtin;if/elseturns the numbers into a sentence.