Practical Examples
1. Data Processing in Scientific Computing
Temperature Analysis
temperature_data = [
('New York', 25.5, 'Celsius'),
('London', 18.3, 'Celsius'),
('Tokyo', 30.2, 'Celsius')
]
def convert_to_fahrenheit(data):
return [
(city, round(temp * 9/5 + 32, 2), 'Fahrenheit')
for city, temp, unit in data
]
converted_temps = convert_to_fahrenheit(temperature_data)
print(converted_temps)
2. Student Grade Management
student_records = [
('Alice', 85, 92, 88),
('Bob', 76, 85, 79),
('Charlie', 90, 88, 95)
]
def calculate_average_grade(records):
return [
(name, round(sum(grades)/len(grades), 2))
for name, *grades in records
]
average_grades = calculate_average_grade(student_records)
print(average_grades)
3. E-commerce Product Inventory
product_inventory = [
('Laptop', 500, 10),
('Smartphone', 300, 15),
('Tablet', 200, 20)
]
def apply_discount(inventory, discount_rate):
return [
(name, price * (1 - discount_rate), quantity)
for name, price, quantity in inventory
]
discounted_inventory = apply_discount(product_inventory, 0.1)
print(discounted_inventory)
graph TD
A[Input Tuple List] --> B[Transformation Function]
B --> C{Process Each Tuple}
C --> D[Apply Calculations]
D --> E[Generate New List]
E --> F[Return Transformed Data]
Example |
Input Size |
Time Complexity |
Space Complexity |
Temperature Conversion |
Small |
O(n) |
O(n) |
Grade Calculation |
Medium |
O(n*m) |
O(n) |
Inventory Discount |
Large |
O(n) |
O(n) |
Best Practices
- Use list comprehensions for concise transformations
- Leverage tuple unpacking for readability
- Consider memory efficiency with large datasets
- Implement type hints for better code documentation
By exploring these practical examples, you'll develop advanced skills in list of tuples manipulation in LabEx Python programming environments.