Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图得到一个特定的温度范围,同时也保持在其他两个变量的范围内_Python_Text Files - Fatal编程技术网

Python 试图得到一个特定的温度范围,同时也保持在其他两个变量的范围内

Python 试图得到一个特定的温度范围,同时也保持在其他两个变量的范围内,python,text-files,Python,Text Files,我知道“湿度”和“风”是没有定义的,我所尝试的可能是完全错误的。我被如何获得第一列难住了,这将是需要在特定范围内的“Temp” 例如,最低温度=60 最高温度=85 同时考虑2个预设条件 湿度必须在70到40之间,风力必须低于12 提前感谢所有的帮助!! 我建议使用。这不仅会使解析文本文件变得更容易,而且dataframes还有一些方法可以帮助您根据所需的任何条件选择数据。使用pandas,您的代码可以变得更简单: min_desired =int(input("Min. Desired Tem

我知道“湿度”和“风”是没有定义的,我所尝试的可能是完全错误的。我被如何获得第一列难住了,这将是需要在特定范围内的“Temp”

例如,最低温度=60 最高温度=85

同时考虑2个预设条件 湿度必须在70到40之间,风力必须低于12

提前感谢所有的帮助!! 我建议使用。这不仅会使解析文本文件变得更容易,而且dataframes还有一些方法可以帮助您根据所需的任何条件选择数据。使用pandas,您的代码可以变得更简单:

min_desired =int(input("Min. Desired Temp.: "))
max_desired = int(input("Man. Desired Temp.: "))

def desired(min_desired,max_desired):
    holder= []
    count = 0
    total = 0
    with open('C:/Users/amaya/OneDrive/Desktop/Weather_final.txt','r') as weather_contents:
        weather = weather_contents.readlines()
        for lines in weather:
            #Use map to convert values on each line to float and to list
            column = list(map(float, lines.strip().split()))
            holder.append(column)

        print(holder)
        for x in holder:
            print(x)
            if x >= min_desired and x <= max_desired:
                if humidity < 70 and humidity > 40:
                    if wind < 12:
                            count +=1
                            total += x
                            avg = (total/ count)
                            print(count)
                            print (avg)

print(desired(min_desired, max_desired))
选择风<12和40<湿度<70的数据:

import pandas as pd
weather = pd.read_csv('weather.txt', sep=" ", names=['Temperature', 'Humidity', 'Wind'])
通常我会使用pandas,因为它需要更简单的代码,并且它还有许多其他有用的函数

但在这里,我将展示如何在没有熊猫的情况下实现这一点——但我没有你的数据来测试它

我将使用一个for循环,然后我可以直接将列或行转换为变量

subset = weather.loc[(weather['Wind']>12) & (weather['Humidity']>40) & (weather['Humidity']<70)]

文本文件中的列名是什么?始终将数据放在文本中,而不是图像中-Python无法从图像中读取数据,因此我们无法测试此代码。列表中包含数据,因此您应该使用x[0],x[1],x[2],而不是x,湿度和风。感谢您抽出时间
temp, humidity, wind = column
# --- functions ---

def desired(min_desired,max_desired):
    #data  = []
    count = 0
    total = 0
    with open('C:/Users/amaya/OneDrive/Desktop/Weather_final.txt','r') as weather_contents:
        for line in weather_contents:
            row = list(map(float, line.strip().split()))
            #data.append(row)
            temp, humidity, wind = row
            if min_desired <= temp <= max_desired and 40 < humidity < 70 and wind < 12:
                count += 1
                total += temp

        print('count:', count)
        if count != 0:
            print('avg:', total/count) # don't divide by zero

# --- main ---

min_desired = int(input("Min. Desired Temp.: "))
max_desired = int(input("Man. Desired Temp.: "))

# without `print()` if you use `print()` inside function
desired(min_desired, max_desired)