forked from balapriyac/python-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4_examples.py
More file actions
51 lines (37 loc) · 1.12 KB
/
day4_examples.py
File metadata and controls
51 lines (37 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
data = [
{"name": "Alice", "city": "London"},
{"name": "Bob", "city": "Paris"},
{"name": "Eve", "city": "London"},
{"name": "John", "city": "New York"},
{"name": "Dana", "city": "Paris"},
]
city_counts = {}
for person in data:
city = person["city"]
if city not in city_counts:
city_counts[city] = 1
else:
city_counts[city] += 1
print(city_counts)
salaries = [
{"role": "Engineer", "salary": 75000},
{"role": "Analyst", "salary": 62000},
{"role": "Engineer", "salary": 80000},
{"role": "Manager", "salary": 95000},
{"role": "Analyst", "salary": 64000},
]
totals = {}
counts = {}
for person in salaries:
role = person["role"]
salary = person["salary"]
totals[role] = totals.get(role, 0) + salary
counts[role] = counts.get(role, 0) + 1
averages = {role: totals[role] / counts[role] for role in totals}
print(averages)
ages = [29, 34, 29, 41, 34, 29]
freq = {}
for age in ages:
freq[age] = freq.get(age, 0) + 1
most_common = max(freq.items(), key=lambda x: x[1])
print(f"Most common age: {most_common[0]} (appears {most_common[1]} times)")