【问题标题】:How to iterate over values of one list, change that with a function and add that to a second list?如何迭代一个列表的值,使用函数更改它并将其添加到第二个列表?
【发布时间】:2019-06-27 00:33:15
【问题描述】:

我有这个温度列表:

temp_data =  [19, 21, 21, 21, 23, 23, 23, 21, 19, 21, 19, 21, 23, 27, 27, 28, 30, 30, 32, 32, 32, 32, 34, 34,
         34, 36, 36, 36, 36, 36, 36, 34, 34, 34, 34, 34, 34, 32, 30, 30, 30, 28, 28, 27, 27, 27, 23, 23,
         21, 21, 21, 19, 19, 19, 18, 18, 21, 27, 28, 30, 32, 34, 36, 37, 37, 37, 39, 39, 39, 39, 39, 39,
         41, 41, 41, 41, 41, 39, 39, 37, 37, 36, 36, 34, 34, 32, 30, 30, 28, 27, 27, 25, 23, 23, 21, 21,
         19, 19, 19, 18, 18, 18, 21, 25, 27, 28, 34, 34, 41, 37, 37, 39, 39, 39, 39, 41, 41, 39, 39, 39,
         39, 39, 41, 39, 39, 39, 37, 36, 34, 32, 28, 28, 27, 25, 25, 25, 23, 23, 23, 23, 21, 21, 21, 21,
         19, 21, 19, 21, 21, 19, 21, 27, 28, 32, 36, 36, 37, 39, 39, 39, 39, 39, 41, 41, 41, 41, 41, 41,
         41, 41, 41, 39, 37, 36, 36, 34, 32, 30, 28, 28, 27, 27, 25, 25, 23, 23, 23, 21, 21, 21, 19, 19,
         19, 19, 19, 19, 21, 23, 23, 23, 25, 27, 30, 36, 37, 37, 39, 39, 41, 41, 41, 39, 39, 41, 43, 43,
         43, 43, 43, 43, 43, 43, 43, 39, 37, 37, 37, 36, 36, 36, 36, 34, 32, 32, 32, 32, 30, 30, 28, 28,
         28, 27, 27, 27, 27, 25, 27, 27, 27, 28, 28, 28, 30, 32, 32, 32, 34, 34, 36, 36, 36, 37, 37, 37,
         37, 37, 37, 37, 37, 37, 36, 34, 30, 30, 27, 27, 25, 25, 23, 21, 21, 21, 21, 19, 19, 19, 19, 19,
         18, 18, 18, 18, 18, 19, 23, 27, 30, 32, 32, 32, 32, 32, 32, 34, 34, 34, 34, 34, 36, 36, 36, 36,
         36, 32, 32, 32, 32, 32, 32, 32, 32, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 28, 28]

我已经导入了一个我创建的具有两个功能的模块,将温度从华氏温度更改为摄氏温度,另一个根据摄氏温度分为 4 类。

from temp_functions import fahr_to_celsius, temp_classifier

所以,我还创建了一个空列表,按照摄氏度分类:

temp_classes =[]

然后是for循环:

for 循环应该遍历 temp_data 中的所有值,并通过以下方式更改它们

fahr_to_celsius 函数,然后将它们附加到空列表 temp_celsius。

for t in temp_data:
    temp_celsius = []
    temp_celsius.append(fahr_to_celsius(t))

问题:我只得到第一个值。我尝试了 range、len、=+1 和其他几个但没有运气。

编辑: 从 OP 评论中添加信息:

这是我正在处理的一项任务:

遍历 temp_data 列表中的华氏温度值 (一一)并在循环内部:创建一个名为的新变量 temp_celsius,您应该在其中指定摄氏温度 使用 fahr_to_celsius 函数转换华氏温度 温度成摄氏度。在中创建一个名为 temp_class 的新变量 您应该为其分配温度等级编号(0、1、2 或 3) 使用 temp_classifier 函数将 temp_class 值添加到 temp_classes 列表

【问题讨论】:

    标签: python list


    【解决方案1】:

    另一种策略是使用 Python 列表推导:

    temp_celsius = [fahr_to_celsius(t) for t in temp_data]

    【讨论】:

    • 谢谢。我也会试试的。现在让自己学习了几个小时。
    【解决方案2】:

    您将在每次迭代中创建一个新列表。将列表的创建移到 for 循环之外:

    temp_celsius = []
    for t in temp_data:
        temp_celsius.append(fahr_to_celsius(t))
    

    【讨论】:

    • 谢谢。但因为这是我正在处理的任务。 """在 temp_data 列表(一个接一个)和循环内迭代华氏温度值:创建一个名为 temp_celsius 的新变量,您应该在其中使用 fahr_to_celsius 函数将华氏温度转换为摄氏度,以摄氏度为单位分配温度。创建一个名为 temp_class 的新变量,您应该在其中使用 temp_classifier 函数分配温度等级编号(0、1、2 或 3) 将 temp_class 值添加到 temp_classes 列表"""
    • @MatijaKordic 您应该将该信息添加到问题中。根据您提供的信息,此答案是正确的。
    • 但是我们也不能为你做作业;接受这个答案并使用它,看看您是否可以将其应用于您的问题。
    • 我知道答案是正确的。我没说不是。只是还在学习在这里写 cmets,因为输入意味着添加评论......谢谢 Cid 和 @thumbtackthief 没有做我的功课,我有硕士学位,刚开始独立学习 python,我没有人可以问,因为课程是关于 git 和它可以访问,但我不能问任何事情,因为我不是他们大学的一部分。所以,我想知道怎么做。
    • 无论如何,我向大家道歉。将在我的结构上工作。
    【解决方案3】:

    您可以考虑使用pandas 来获得表格视图

    import pandas as pd
    df = pd.DataFrame({"fahr":temp_data})
    
    df["celsius"] = df["fahr"].apply(fahr_to_celsius)
    # or
    df["celsius"] = fahr_to_celsius(df["fahr"])
    # or (even faster)
    df["celsius"] = fahr_to_celsius(df["fahr"].values)
    
    

    【讨论】:

    • 谢谢。也会试试的。我是一个完全的新手,所以学习不同的做事方式很棒。
    【解决方案4】:

    您也可以使用 map 来执行此操作,尽管有些人不认为这是“pythonic”

    map() 将函数映射到迭代中的每个项目,并采用

    的形式
    map(function, iterable) #where an iterable is a list, set, tuple
    

    它本质上与列表推导相同。

    python3:

    temp_celcius = list(map(fahr_to_celsius, temp_data)) # explicitly call it a list
    

    python2:

    temp_celcius = map(fahr_to_celsius, temp_data) # interpreted as a list
    

    【讨论】:

    • 谢谢。从来没有听说过这个。我一定会玩弄它以了解它的功能。
    猜你喜欢
    • 2020-05-24
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    相关资源
    最近更新 更多