File size: 1,181 Bytes
a89f25d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Python Example: Data Processing and File Operations
import json

# Sample data
data = [
    {"name": "Alice", "age": 25, "city": "New York"},
    {"name": "Bob", "age": 30, "city": "San Francisco"},
    {"name": "Charlie", "age": 35, "city": "Boston"},
    {"name": "Diana", "age": 28, "city": "Chicago"},
    {"name": "Eve", "age": 22, "city": "New York"}
]

print("=== Data Processing Demo ===\n")

# Filter people over 27
older_people = [person for person in data if person["age"] > 27]
print(f"People over 27: {len(older_people)}")

# Group by city
cities = {}
for person in data:
    city = person["city"]
    if city not in cities:
        cities[city] = []
    cities[city].append(person)

print("\nPeople by city:")
for city, people in cities.items():
    print(f"  {city}: {len(people)} people")

# Calculate average age
total_age = sum(person["age"] for person in data)
avg_age = total_age / len(data)
print(f"\nAverage age: {avg_age:.2f}")

# Find youngest and oldest
youngest = min(data, key=lambda x: x["age"])
oldest = max(data, key=lambda x: x["age"])

print(f"Youngest: {youngest['name']} ({youngest['age']})")
print(f"Oldest: {oldest['name']} ({oldest['age']})")