学校作业中遇到多个列表问题

-3 投票
2 回答
969 浏览
提问于 2025-04-18 03:30

我现在正在上一个Python课程,但在处理列表时遇到了很多问题。我想尽可能“高效”地写这个程序,也就是说,尽量少写代码行数和减少处理能力的使用。

到目前为止,我有:

import os
import functools
file = input("Input file name(.txt):   ")

lstData=[]
if(os.path.isfile(file)):
    weather = open(file, 'r')
    for line in weather:
        line = line.strip()
        line = line.split(",")

        for i in range(len(line)):
            if(line[i].isdigit()):
                line[i] = int(line[i])
        lstData.append(line)
Jan = ((lstData[0][1:]))

    F=c*1.8+32
    return(F)
def average(values):
    length=len(values)
    total_sum=0

    for i in range(length):
        total_sum+=sum(values)
    total_sum =sum(values)
    average= float(total_sum/length)

这个项目的规则如下:

我生成了一些随机的温度读数,单位是摄氏度,并把这些数据存储在一个叫“weather.txt”的文件里。请完成以下任务:

  • 创建一个函数,从“weather.txt”文件中加载样本天气数据,并将其存储到合适的数据结构中——数值数据应该转换为合适的数据类型,并将这些值返回给调用该函数的地方。
  • 创建一个函数来计算这个数值列表的平均值(在这个例子中是温度数据)。
  • 创建一个函数,将摄氏度转换为华氏度。再创建两个函数,能够找到一组数值中的最高和最低温度。
  • 使用上面开发的函数,创建一个程序来完成以下任务:
    • 创建一个简单的菜单系统来完成以下操作:
    • 提示用户输入要加载的文件
    • 如果文件存在,则将数据加载到一个列表对象中
    • 如果文件不存在,提示用户输入一个真实的文件名
    • 显示所有月份的平均温度,单位为摄氏度和华氏度
    • 显示每个月的最高和最低温度
    • 退出程序

数据:

Jan, -2,-5,-6,-10,2,1,6,-2,3,1,0,1
Feb,-6,-11,-5,-4,-2,-1,0,2,5,3,5,4
Mar,5,8,9,10,13,15,18,10,12,13,11
Apr,15,17,19,15,16,18,19,15,17,14
May,18,19,23,22,25,21,20,19,22,25
June,25,28,27,29,30,35,33,32,31,34,33
Jul,33,32,36,37,40,41,42,45,41,39,37,40
Aug,40,41,421,43,39,45,43,39,39,40,41,42
Sep,38,37,36,33,35,29,28,29,25,23,26,27,30
Oct,27,24,24,20,22,19,18,20,21,18,17,15,18,21
Nov,16,19,14,15,12,15,11,10,9,10,6,11,8,7,5,3
Dec,2,5,6,1,7,8,3,2,-1,0,2,-2,1,0,-2,-4,1,0,-2,-1,0

我并不是让任何人来做这个,但如果有人对列表有很好的了解,我会非常感激。

我想创建一些函数,可以去掉列表开头的字符串,然后把剩下的字符串转换成浮点数或整数。但到现在为止我还没有成功。

我也不能使用任何给定的函数,比如max、min、average等,必须自己写。这并不是一个大问题,只是我在处理列表时的能力不足。如果这有影响的话,我会使用Python 3.3/3.4。谢谢。

2 个回答

0
# assumes Python 3.x
import os

def get_filename(prompt):
    while True:
        fname = input(prompt)
        if os.path.isfile(fname):
            return fname

def load_weather(fname):
    weather = []
    with open(fname) as inf:
        for line in inf:
            month, temps = line.split(",", 1)
            temps = [float(temp) for temp in temps.rstrip().split(",")]
            weather.append((month, temps))
    return weather

def f_to_c(f):
    return (f - 32.) / 1.8

def c_to_f(c):
    return c * 1.8 + 32.

def average(lst):
    return sum(lst) / len(lst)

def main():
    fname = get_filename("Enter the name of your weather data file: ")
    weather = load_weather(fname)

    print("Month    Low   Avg  High")
    for month,temps in weather:
        print("{:<5} {:>5.1f} {:>5.1f} {:>5.1f}".format(month, min(temps), average(temps), max(temps)))

if __name__=="__main__":
   main()

当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。

0

你可以把你的数据读入一个字典,里面包含一些列表,像这样:

def read_temps(filename):
  temps = {}
  f = open(filename, "r")
  line = f.readline()
  while line != "":
    a = line.split(",")
    temps[a[0].strip()] = [float(x) for x in a[1:]]
    line = f.readline()
  f.close()
  return temps

temps = read_temps("temps.txt")
print temps

输出结果:

{'Mar': [5.0, 8.0, 9.0, 10.0, 13.0, 15.0, 18.0, 10.0, 12.0, 13.0, 11.0],
 'Feb': [-6.0, -11.0, -5.0, -4.0, -2.0, -1.0, 0.0, 2.0, 5.0, 3.0, 5.0, 4.0],
 'Aug': [40.0, 41.0, 421.0, 43.0, 39.0, 45.0, 43.0, 39.0, 39.0, 40.0, 41.0, 42.0],
 'Sep': [38.0, 37.0, 36.0, 33.0, 35.0, 29.0, 28.0, 29.0, 25.0, 23.0, 26.0, 27.0, 30.0],
 'Apr': [15.0, 17.0, 19.0, 15.0, 16.0, 18.0, 19.0, 15.0, 17.0, 14.0],
 'June': [25.0, 28.0, 27.0, 29.0, 30.0, 35.0, 33.0, 32.0, 31.0, 34.0, 33.0],
 'Jul': [33.0, 32.0, 36.0, 37.0, 40.0, 41.0, 42.0, 45.0, 41.0, 39.0, 37.0, 40.0],
 'Jan': [-2.0, -5.0, -6.0, -10.0, 2.0, 1.0, 6.0, -2.0, 3.0, 1.0, 0.0, 1.0],
 'May': [18.0, 19.0, 23.0, 22.0, 25.0, 21.0, 20.0, 19.0, 22.0, 25.0],
 'Nov': [16.0, 19.0, 14.0, 15.0,12.0, 15.0, 11.0, 10.0, 9.0, 10.0, 6.0, 11.0, 8.0, 7.0, 5.0, 3.0],
 'Dec': [2.0, 5.0, 6.0, 1.0, 7.0, 8.0, 3.0, 2.0, -1.0, 0.0, 2.0, -2.0, 1.0, 0.0, -2.0, -4.0, 1.0, 0.0, -2.0, -1.0, 0.0],
 'Oct': [27.0, 24.0, 24.0, 20.0, 22.0, 19.0, 18.0, 20.0, 21.0, 18.0, 17.0, 15.0, 18.0, 21.0]}

撰写回答