Python 如何在.txt文件中拆分数字和单词

Python 如何在.txt文件中拆分数字和单词,python,file,dictionary,Python,File,Dictionary,tests1.txt是: 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 我的问题是:我需要找到使用这个.txt文件的每个房间的平均值。我如何将数字和房间分开,以便使用以下方法找到平均温度: dict={} number={} for y in

tests1.txt是:

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
我的问题是:我需要找到使用这个.txt文件的每个房间的平均值。我如何将数字和房间分开,以便使用以下方法找到平均温度:

dict={} 
number={} 
for y in (tempts1.txt) as file:
  number = int(y[1])
  room=y[0]
  if room not in dict:
    room=number
  else: room+=number
for y in dict and number:
   print(dict[y],':',number[y])

这是一种使用简单迭代的方法

演示:

from collections import defaultdict

res = defaultdict(list)
with open(filename) as infile:
    for line in infile:                    #Iterate Each line
        val = line.strip().split(",")      #Split line by comma
        res[val[0]].append(int(val[1]))

for k, v in res.items():
    print("Room: {}, Avg: {}".format(k, sum(v)/float(len(v))))
Room: Living Room, Avg: 24.0
Room: Kitchen, Avg: 22.5
Room: Bedroom, Avg: 25.5
根据注释编辑,无需导入

res = {}
with open(filename) as infile:
    for line in infile:                    #Iterate Each line
        val = line.strip().split(",")      #Split line by comma
        if val[0] not in res:
            res[val[0]] = []
        res[val[0]].append(int(val[1]))

for k, v in res.items():
    print("Room: {}, Avg: {}".format(k, sum(v)/float(len(v))))
输出:

from collections import defaultdict

res = defaultdict(list)
with open(filename) as infile:
    for line in infile:                    #Iterate Each line
        val = line.strip().split(",")      #Split line by comma
        res[val[0]].append(int(val[1]))

for k, v in res.items():
    print("Room: {}, Avg: {}".format(k, sum(v)/float(len(v))))
Room: Living Room, Avg: 24.0
Room: Kitchen, Avg: 22.5
Room: Bedroom, Avg: 25.5

这是一种使用简单迭代的方法

演示:

from collections import defaultdict

res = defaultdict(list)
with open(filename) as infile:
    for line in infile:                    #Iterate Each line
        val = line.strip().split(",")      #Split line by comma
        res[val[0]].append(int(val[1]))

for k, v in res.items():
    print("Room: {}, Avg: {}".format(k, sum(v)/float(len(v))))
Room: Living Room, Avg: 24.0
Room: Kitchen, Avg: 22.5
Room: Bedroom, Avg: 25.5
根据注释编辑,无需导入

res = {}
with open(filename) as infile:
    for line in infile:                    #Iterate Each line
        val = line.strip().split(",")      #Split line by comma
        if val[0] not in res:
            res[val[0]] = []
        res[val[0]].append(int(val[1]))

for k, v in res.items():
    print("Room: {}, Avg: {}".format(k, sum(v)/float(len(v))))
输出:

from collections import defaultdict

res = defaultdict(list)
with open(filename) as infile:
    for line in infile:                    #Iterate Each line
        val = line.strip().split(",")      #Split line by comma
        res[val[0]].append(int(val[1]))

for k, v in res.items():
    print("Room: {}, Avg: {}".format(k, sum(v)/float(len(v))))
Room: Living Room, Avg: 24.0
Room: Kitchen, Avg: 22.5
Room: Bedroom, Avg: 25.5

对一个字典求和,对另一个字典计数,取平均值:

from collections import defaultdict

d = defaultdict(int)
count = defaultdict(int)

lines = txt.split('\n')
for line in lines:
    sep = line.split(',')
    d[sep[0]] += int(sep[1])
    count[sep[0]] += 1 

for k, v in d.items():
    print(f'Average of {k}: {v/count[k]}')

# Average of Living Room: 24.0
# Average of Bedroom: 25.5                                        
# Average of Kitchen: 22.5

对一个字典求和,对另一个字典计数,取平均值:

from collections import defaultdict

d = defaultdict(int)
count = defaultdict(int)

lines = txt.split('\n')
for line in lines:
    sep = line.split(',')
    d[sep[0]] += int(sep[1])
    count[sep[0]] += 1 

for k, v in d.items():
    print(f'Average of {k}: {v/count[k]}')

# Average of Living Room: 24.0
# Average of Bedroom: 25.5                                        
# Average of Kitchen: 22.5
给予

给予


使用适合您的库?使用适合您的库?我是否能够使用不需要导入函数的库。我是否能够使用不需要导入函数的库。