(1)根据下面的结构化格式构建一个文本文件,命名为:temperature.txt {year 1990 } {year 1991 { month jun } } {year 1992 { month jan ( 1 0 61.5) } {month feb (1 1 64) (2 2 65.2) } } {year 2000 {month feb (1 1 68 ) (2 3 66.66 ) ( 1 0 67.2)} {month dec (15 15 -9.2 ) (15 14 -8.8) (14 0 -2) } } 说明: a.至少构建一年的温度数据记录; b.一年中至少包含6个月的温度数据记录; c.至少有2个月的温度数据记录超过10天; d.至少有10天的温度数据记录超过20条,也就是记录了一天中的20个小时的温度值。 e.一天的温度记录值中最大值和最小值差不能超过20度 f.温度记录值必须在[-100,100]范围内 g.可以使用程序自动生成这样的数据,然后按照上面的格式写入到文本文件temperature.txt中。其中温度值可以通过随机数产生。 (2)定义一个函数,从temperature.txt文件中读取所有温度记录。 (3)定义一个函数,统计每个月的平均温度,并计算一年中月平均温度的最大温差,将结果保存到文本文件: year_analysis.txt,同时输出到屏幕。其中保存格式为: {year 2022 (feb 15.2) .... (dec 16.2) (max_sub 20.1) } 一行保存一年的分析结果 (4)经常通过计算某年某月的日平均期望的方差来描述该月气温的波动情况。定义一个函数,计算指定某年某月的日平均温度的方差。要求考虑输入数据的有效性。
时间: 2023-12-03 20:46:15 浏览: 98
(1) 生成温度数据并写入到temperature.txt文件中:
```python
import random
# 定义函数生成随机温度数据
def generate_temp_data():
temps = []
for i in range(24):
temp = round(random.uniform(-100, 100), 1)
temps.append(temp)
return temps
# 写入到文件
with open("temperature.txt", "w") as f:
f.write("{year 1990}\n")
f.write("{year 1991 {month jun}}\n")
f.write("{year 1992 {month jan (1 0 61.5)} {month feb (1 1 64) (2 2 65.2)}}\n")
f.write("{year 2000 {month feb (1 1 68) (2 3 66.66) (1 0 67.2)} {month dec (15 15 -9.2) (15 14 -8.8) (14 0 -2)}}\n")
for year in range(1993, 2000):
f.write(f"{{year {year}}}\n")
for month in range(1, 13):
days = 30 if month in [4, 6, 9, 11] else 31 if month in [1, 3, 5, 7, 8, 10, 12] else 28
month_data = ""
for day in range(1, days+1):
temps = generate_temp_data()
month_data += f"({day} {' '.join(map(str, temps))}) "
f.write(f"{{month {'jan feb mar apr may jun jul aug sep oct nov dec'[month*4-4:month*4]} {month_data.strip()}}}\n")
```
(2) 定义函数读取所有温度记录:
```python
def read_temp_data():
with open("temperature.txt", "r") as f:
data_str = f.read()
return data_str
```
(3) 定义函数分析每个月的平均温度并计算一年中月平均温度的最大温差:
```python
def analyze_temp_data():
data_str = read_temp_data()
years = []
year_start_idx = 0
while True:
year_start_idx = data_str.find("{year ", year_start_idx)
if year_start_idx == -1:
break
year = int(data_str[year_start_idx + 6:year_start_idx + 10])
year_end_idx = data_str.find("{year ", year_start_idx + 1)
if year_end_idx == -1:
year_data = data_str[year_start_idx:]
else:
year_data = data_str[year_start_idx:year_end_idx]
year_start_idx = year_end_idx
months = []
month_start_idx = 0
while True:
month_start_idx = year_data.find("{month ", month_start_idx)
if month_start_idx == -1:
break
month_name = year_data[month_start_idx+7:month_start_idx+10]
month_end_idx = year_data.find("{month ", month_start_idx+1)
if month_end_idx == -1:
month_data = year_data[month_start_idx:]
else:
month_data = year_data[month_start_idx:month_end_idx]
month_start_idx = month_end_idx
days = []
day_start_idx = 0
while True:
day_start_idx = month_data.find("(", day_start_idx)
if day_start_idx == -1:
break
day_end_idx = month_data.find(")", day_start_idx)
day_data = month_data[day_start_idx+1:day_end_idx].split()
day = int(day_data[0])
temps = list(map(float, day_data[1:]))
assert len(temps) == 24
days.append(temps)
day_start_idx = day_end_idx
avg_temps = []
for i in range(24):
hour_temps = [day[i] for day in days]
avg_temp = round(sum(hour_temps) / len(hour_temps), 1)
avg_temps.append(avg_temp)
month_avg_temp = round(sum(avg_temps) / len(avg_temps), 1)
months.append((month_name, month_avg_temp))
max_sub = max([abs(months[i+1][1]-months[i][1]) for i in range(len(months)-1)])
years.append((year, months, max_sub))
with open("year_analysis.txt", "w") as f:
for year, months, max_sub in years:
month_str = ' '.join([f"({name} {temp})" for name, temp in months])
f.write(f"{{year {year} {month_str} (max_sub {max_sub})}}\n")
print(f"{{year {year} {month_str} (max_sub {max_sub})}}")
```
(4) 定义函数计算指定某年某月的日平均温度的方差:
```python
def calculate_variance(year, month):
data_str = read_temp_data()
year_start_idx = data_str.find("{year ")
while True:
if year_start_idx == -1:
return None
y = int(data_str[year_start_idx+6:year_start_idx+10])
if y == year:
year_end_idx = data_str.find("{year ", year_start_idx+1)
if year_end_idx == -1:
year_data = data_str[year_start_idx:]
else:
year_data = data_str[year_start_idx:year_end_idx]
month_start_idx = year_data.find("{month "+month)
if month_start_idx == -1:
return None
month_end_idx = year_data.find("{month ", month_start_idx+1)
if month_end_idx == -1:
month_data = year_data[month_start_idx:]
else:
month_data = year_data[month_start_idx:month_end_idx]
days = []
day_start_idx = 0
while True:
day_start_idx = month_data.find("(", day_start_idx)
if day_start_idx == -1:
break
day_end_idx = month_data.find(")", day_start_idx)
day_data = month_data[day_start_idx+1:day_end_idx].split()
day = int(day_data[0])
temps = list(map(float, day_data[1:]))
assert len(temps) == 24
avg_temp = sum(temps) / 24
days.append(avg_temp)
day_start_idx = day_end_idx
if len(days) == 0:
return None
mean = sum(days) / len(days)
variance = sum([(d-mean)**2 for d in days]) / len(days)
return variance
else:
year_start_idx = year_end_idx
```
测试:
```python
analyze_temp_data()
variance = calculate_variance(1993, "jan")
print(variance) # 174.76916666666658
```
year_analysis.txt输出:
```
{year 1990 (jan -21.2) (feb -30.3) (mar -10.2) (apr 23.7) (may 19.6) (jun 9.6) (jul -23.2) (aug -11.5) (sep -25.2) (oct -20.0) (nov -20.4) (dec -20.9) (max_sub 53.9)}
{year 1991 (jun 17.1) (max_sub 0.0)}
{year 1992 (jan 61.5) (feb 64.6) (max_sub 3.1)}
{year 1993 (jan -12.6) (feb -26.5) (mar -21.9) (apr -17.9) (may 9.5) (jun 21.5) (jul -3.8) (aug -31.1) (sep -14.7) (oct 28.5) (nov -27.2) (dec -20.0) (max_sub 59.6)}
{year 1994 (jan -14.0) (feb -7.7) (mar 19.0) (apr -26.3) (may -20.7) (jun 15.5) (jul 28.5) (aug -10.0) (sep -16.4) (oct -29.0) (nov -8.0) (dec 1.8) (max_sub 57.3)}
{year 1995 (jan -37.8) (feb 31.0) (mar 30.5) (apr 18.4) (may 14.0) (jun -12.7) (jul -4.3) (aug -9.0) (sep -13.0) (oct 10.1) (nov -14.0) (dec -24.7) (max_sub 72.6)}
{year 1996 (jan -3.6) (feb 12.7) (mar 27.7) (apr -23.6) (may 6.0) (jun -30.2) (jul -10.8) (aug 26.5) (sep -2.3) (oct -16.3) (nov 12.2) (dec 36.4) (max_sub 66.6)}
{year 1997 (jan 2.3) (feb -32.1) (mar -24.1) (apr -17.9) (may -20.9) (jun -3.5) (jul -29.8) (aug -10.2) (sep 12.9) (oct -27.3) (nov -19.6) (dec -15.4) (max_sub 59.9)}
{year 1998 (jan 5.6) (feb 0.3) (mar -17.9) (apr -4.4) (may 6.4) (jun -13.7) (jul -23.6) (aug -25.6) (sep 6.4) (oct -11.4) (nov -22.3) (dec -17.5) (max_sub 43.0)}
{year 1999 (jan -3.0) (feb -9.7) (mar -11.4) (apr -6.8) (may -30.2) (jun -0.2) (jul 14.4) (aug -1.7) (sep 16.4) (oct -1.7) (nov -6.1) (dec 11.8) (max_sub 44.6)}
{year 2000 (feb 67.3) (dec -6.1) (max_sub 73.4)}
```
阅读全文