我在python上学习的网站无法很好地解释字典,因此我求助于此。我应该做的是找到用户键入多少天后这些房间中3个温度的平均值(最大为3)。每天1天有4个温度。当我运行此代码时,这3个房间都不会产生任何温度。
hi={}
days=int(input('How many days of data do you have? '))
print('Average Temperatures:')
f = open('temps1.txt')
g= open('temps1.txt') and open('temps2.txt')
o=open('temps1.txt') and open('temps2.txt') and open('temps3.txt')
if days=='1':
for line in f:
parts = line.strip().split(",")
if parts[0] not in hi:
hi[parts[0]] = []
hi[parts[0]].append(int(val[1]))
elif days=='2':
for line in g:
parts = line.strip().split(",")
if parts[0] not in hi:
hi[parts[0]] = []
hi[parts[0]].append(int(val[1]))
elif days=='3':
for line in o:
parts = line.strip().split(",")
if parts[0] not in hi:
hi[parts[0]] = []
hi[parts[0]].append(int(val[1]))
for k, v in hi.items():
print("Room: {}, Avg: {}".format(k, sum(v)/days*4))
temps1.txt仅在用户在输入问题中键入1时激活:
Living Room,23
Bedroom,24
Kitchen,22
Living Room,24
Bedroom,26
Kitchen,22
Living Room,25
Bedroom,26
Kitchen,23
Living Room,24
Bedroom,26
Kitchen,23
temps2.txt仅在用户在第一个问题中输入2时激活(与temps1.txt一起使用):
Living Room,26
Bedroom,24
Kitchen,23
Living Room,27
Bedroom,24
Kitchen,24
Living Room,28
Bedroom,25
Kitchen,26
Living Room,29
Bedroom,26
Kitchen,28
temps3.txt仅在用户在第一个问题中输入3时激活(与temps1.txt和temps2.txt一起使用):
Living Room,28
Bedroom,29
Kitchen,28
Living Room,28
Bedroom,30
Kitchen,29
Living Room,29
Bedroom,29
Kitchen,31
Living Room,21
Bedroom,23
Kitchen,21
我可以生成一个代码来执行此操作,但是没有这种导入吗?还有一件事,我可以为“ for in”和“ if”语句分配变量吗?
答案 0 :(得分:1)
允许拆分数据文件的创建和处理以使其成为minimal verifyable complete example-答案:
def createData():
"""Create demo data as files 'temp1.txt','temp2.txt','temp3.txt'"""
with open("temp1.txt","w") as f:
f.write("""Living Room,23
Bedroom,24\nKitchen,22\nLiving Room,24\nBedroom,26\nKitchen,22\nLiving Room,25\nBedroom,26\nKitchen,23\nLiving Room,24\nBedroom,26\nKitchen,23\n""")
with open("temp2.txt","w") as f:
f.write("""Living Room,26\nBedroom,24\nKitchen,23\nLiving Room,27\nBedroom,24\nKitchen,24\nLiving Room,28\nBedroom,25\nKitchen,26\nLiving Room,29\nBedroom,26\nKitchen,28\n""")
with open("temp3.txt","w") as f:
f.write("""Living Room,28\nBedroom,29\nKitchen,28\nLiving Room,28\nBedroom,30\nKitchen,29\nLiving Room,29\nBedroom,29\nKitchen,31\nLiving Room,21\nBedroom,23\nKitchen,21\n""")
使用此演示数据,我们开始用户输入和计算。我们将所有需要的数据从1到3个文件读取到一个字典中并输出:
# create all data files
createData()
# process data
nrDays = 0
# ask for how many days, catch invalid values until valid one is given
while nrDays not in range(1,4):
try:
nrDays=input('How many days of data do you have? [1-3] ')
nrDays=int(nrDays)
except: # catchall
print("Parsing error. 1 to 3 allowed. {} given".format(nrDays))
# read data only from files needed
temps = {}
for d in range(1,nrDays+1):
# format file names as temp1.txt to temp3.txt as needed
with open ("temp{}.txt".format(d),"r") as f: # open file
for line in f:
if line.strip(): # if line not empty, f.e. dangling newline at end
room,temp = line.strip().split(",")
temp = int(temp) # convert to int
k = temps.setdefault(room,[]) # creates the empty list if needed
# returns the value if present
k.append(temp)
print('Average Temperatures:')
for k, v in temps.items():
print("Room: {}, Avg: {}".format(k, sum(v)/len(v))) # get the length from the list
输出:
How many days of data do you have? 3
Average Temperatures:
Room: Living Room, Avg: 26.0
Room: Bedroom, Avg: 26.0
Room: Kitchen, Avg: 25.0
How many days of data do you have? 2
Average Temperatures:
Room: Living Room, Avg: 25.75
Room: Bedroom, Avg: 25.125
Room: Kitchen, Avg: 23.875
How many days of data do you have? 1
Average Temperatures:
Room: Living Room, Avg: 24.0
Room: Bedroom, Avg: 25.5
Room: Kitchen, Avg: 22.5
如果您输入1-3以外的任何内容,则会得到一个新的输入提示(如果输入了int)或一条消息以及一个新的提示(如果没有给出int)。