Data Processing Techniques
String Manipulation
## Convert names to uppercase
names = ['alice', 'bob', 'charlie']
uppercase_names = list(map(str.upper, names))
print(uppercase_names) ## Output: ['ALICE', 'BOB', 'CHARLIE']
## Complex numeric operations
temperatures = [0, 32, 100]
celsius_to_fahrenheit = list(map(lambda c: (c * 9/5) + 32, temperatures))
print(celsius_to_fahrenheit) ## Output: [32.0, 89.6, 212.0]
Working with Complex Objects
## Transform dictionary values
users = [
{'name': 'alice', 'age': 30},
{'name': 'bob', 'age': 25}
]
user_names = list(map(lambda user: user['name'], users))
print(user_names) ## Output: ['alice', 'bob']
## Combine multiple lists
prices = [10, 20, 30]
discounts = [0.1, 0.2, 0.3]
discounted_prices = list(map(lambda p, d: p * (1 - d), prices, discounts))
print(discounted_prices) ## Output: [9.0, 16.0, 21.0]
Practical Scenarios
| Scenario |
Transformation |
Example |
| Data Cleaning |
Type Conversion |
map(float, ['1.1', '2.2', '3.3']) |
| Data Normalization |
Scaling Values |
map(lambda x: x/max(values), values) |
| Formatting |
String Processing |
map(str.strip, [' hello ', 'world ']) |
graph LR
A[Input Data] --> B{map() Function}
B --> C[Transformation Logic]
C --> D[Transformed Output]
D --> E[Further Processing]
## Real-world data processing
def process_student(student):
return {
'name': student['name'].capitalize(),
'grade': 'Pass' if student['score'] >= 60 else 'Fail'
}
students = [
{'name': 'alice', 'score': 75},
{'name': 'bob', 'score': 45}
]
processed_students = list(map(process_student, students))
print(processed_students)
Best Practices
- Use
map() for simple, uniform transformations
- Convert to list when immediate evaluation is needed
- Combine with other functional programming tools
- Keep transformation functions pure and side-effect free
LabEx recommends practicing these transformation techniques to enhance your Python data processing skills.