【问题标题】:Assignment for school and having multiple issues with lists学校分配和列表有多个问题
【发布时间】:2014-04-20 14:20:02
【问题描述】:

我目前正在学习 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

我不是要求任何人这样做,但如果有人对列表有很好的了解,我将非常感激。

我想做一些函数,可以将字符串从列表的开头取出,然后将字符串的其余部分转换为浮点数或整数。我还没有运气。

我也不能使用任何给定的函数或最大值、最小值、平均值等,必须自己制作。这不是一个真正的大问题。这只是我无法使用列表。如果有区别的话,这将在 Python 3.3/3.4 中。谢谢。

【问题讨论】:

  • 欢迎来到 SO。尚不完全清楚您不理解的列表是什么。尽量缩小问题的范围,制定一个更精确、更简短的问题。这样做的目的是双重的; (1) 让问题更容易回答,(2) 让你更好地构建你的想法,甚至可以自己弄清楚。
  • “我正在尝试以最“高效”的方式制作这个程序(使用最少的行数和处理能力)” 一般经验法则:首先让它工作,然后让它更好地工作。
  • 顺便说一句,你应该看看 list comprehension,这是 Python 最伟大的特性之一。
  • @Henrik 不,OP 不应该。如果提问者在使用列表方面需要帮助,那么在这里添加列表理解毫无用处。
  • @SimonT 我不同意。列表推导式可能是 Python 中最常用的列表处理方式,为什么不从一开始就学习呢?目标也是使用尽可能少的行,这肯定是为了促进列表理解的使用。

标签: python list file python-3.x


【解决方案1】:

您可以将数据读入列表字典,如下所示:

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]}

【讨论】:

  • “是的,但是”字典以任意(基本上是随机的)顺序返回。
【解决方案2】:
# 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()

【讨论】:

    猜你喜欢
    • 2016-12-27
    • 2014-11-07
    • 2013-10-23
    • 2017-07-31
    • 1970-01-01
    • 2013-05-02
    • 2010-11-21
    • 1970-01-01
    • 2021-12-08
    相关资源
    最近更新 更多