如何使用MapReduce识别高温日与低温日?
Temperature Data Analysis: Identifying High & Low Temp Days
First, let's organize your raw temperature data into a readable table for clarity:
| Date | High Temp | Low Temp |
|---|---|---|
| 20130101 | 12.8 | 9.6 |
| 20130102 | 10.1 | 3.8 |
| 20130103 | 7.0 | -2.2 |
| 20130104 | 11.8 | -3.7 |
| 20130105 | 8.6 | -1.1 |
| 20130106 | 10.5 | 1.9 |
| 20130107 | 13.4 | -0.1 |
| 20130108 | 16.2 | 1.4 |
| 20130109 | 17.8 | 12.4 |
| 20130110 | 20.0 | 16.2 |
| 20130111 | 15.4 | 5.0 |
Your goal is to flag high-temperature days (max temp >40) and low-temperature days (min temp <10). Looking at your code snippet, it seems you started processing the data but it's incomplete. Let's fix that.
Original Code Snippet
current_date = None current_temp = None for line in data.strip().split('\n'): Mapper_data = ["%s\o%s\o%s" %...
Complete Working Code
Here's a polished version that reads your data, parses each entry, and categorizes the days correctly:
# Raw temperature data data = "20130101 12.8 9.6 20130102 10.1 3.8 20130103 7.0 -2.2 20130104 11.8 -3.7 20130105 8.6 -1.1 20130106 10.5 1.9 20130107 13.4 -0.1 20130108 16.2 1.4 20130109 17.8 12.4 20130110 20.0 16.2 20130111 15.4 5.0" # Split data into chunks of 3 elements (date, high, low) entries = [data.split()[i:i+3] for i in range(0, len(data.split()), 3)] high_temp_days = [] low_temp_days = [] for entry in entries: date, high_str, low_str = entry high_temp = float(high_str) low_temp = float(low_str) if high_temp > 40: high_temp_days.append(date) if low_temp < 10: low_temp_days.append(date) # Output results print("High-temperature days (max >40):", high_temp_days if high_temp_days else "None in sample data") print("Low-temperature days (min <10):", low_temp_days)
Output
When you run this code with your sample data, you'll get:
High-temperature days (max >40): None in sample data Low-temperature days (min <10): ['20130101', '20130102', '20130103', '20130104', '20130105', '20130106', '20130107', '20130108', '20130111']
Key notes about the code:
- We split the flat data string into groups of 3 (date, high, low) since each day has three values.
- Convert temperature strings to floats for numerical comparison.
- Separate lists to track high and low temp days, making it easy to access each category later.
内容的提问来源于stack exchange,提问作者Iriel
相关产品推荐
相关产品推荐

