找出Python中的平均值

发布于 2024-10-23 20:04:04 字数 418 浏览 4 评论 0原文

我正在读取 .txt 文件,每个人都有四个分数,我需要找出如何从每一行中获取分数并找到每个人的平均值。

s={}
results= open("surf.txt")
for line in results:
  (s['Name'], s['scoreOne'], s['scoreTwo'], s['scoreThree'], s['scoreFour']) =line.split(";")

这似乎是解决这个问题所需的所有代码。

surf.txt 包含:

Johnny;8.65;7.32;7.81;9.12
Juan;9.12;8.45;8.80;5.60
Joseph;8.45;9.00;9.12;9.13
Stacey;7.81;8.33;9.00;8.10 
(...)

I'm reading from a .txt file and have four scores for each person, i need to find out how to take the scores from each line and find the average for each person.

s={}
results= open("surf.txt")
for line in results:
  (s['Name'], s['scoreOne'], s['scoreTwo'], s['scoreThree'], s['scoreFour']) =line.split(";")

That seems like all the code that's needed to figure this out.

surf.txt contains:

Johnny;8.65;7.32;7.81;9.12
Juan;9.12;8.45;8.80;5.60
Joseph;8.45;9.00;9.12;9.13
Stacey;7.81;8.33;9.00;8.10 
(...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷乱花海 2024-10-30 20:04:04

解决方案:

separator = ";"
inputFile = "input.txt"

with open(inputFile) as f:
    for line in f:
        values = line.split(separator)
        name = values[0]
        scores = map(float, values[1:])
        avg = sum(scores) / len(scores)
        print name, avg

输入:

Maciej;5;10;15;50
John;15;8;10;14
Mike;5;5;5;5

输出:

Maciej 20.0
John 11.75
Mike 5.0

Solution:

separator = ";"
inputFile = "input.txt"

with open(inputFile) as f:
    for line in f:
        values = line.split(separator)
        name = values[0]
        scores = map(float, values[1:])
        avg = sum(scores) / len(scores)
        print name, avg

Input:

Maciej;5;10;15;50
John;15;8;10;14
Mike;5;5;5;5

Output:

Maciej 20.0
John 11.75
Mike 5.0
烟火散人牵绊 2024-10-30 20:04:04

如果您的文件具有以下格式:

john; 1; 2; 3; 4
pete; 5; 4; 3; 2
joan; 9; 8; 7; 6

那么您可以简单地:

with open('surf.txt', 'rb') as fp:
 for line in fp.readlines():
  tokens = line.strip().split(';') # this creates a list of strings
  name = tokens[0] # extract the first (left-most) string (the name)
  nums = [float(k) for k in tokens[1:]] # convert strings to floats
  mean = sum(nums) / len(nums) # compute the arithmetic mean
  print "%s has a mean of %f" % (name, mean) # print the result

请注意,如果该人有多个姓名,则此示例将不起作用:它假设该姓名只有一列,其余列可以转换为浮点数。

If your file has the following format:

john; 1; 2; 3; 4
pete; 5; 4; 3; 2
joan; 9; 8; 7; 6

Then you can simply:

with open('surf.txt', 'rb') as fp:
 for line in fp.readlines():
  tokens = line.strip().split(';') # this creates a list of strings
  name = tokens[0] # extract the first (left-most) string (the name)
  nums = [float(k) for k in tokens[1:]] # convert strings to floats
  mean = sum(nums) / len(nums) # compute the arithmetic mean
  print "%s has a mean of %f" % (name, mean) # print the result

Notice that this example would NOT work if the person had more than one name: it assumes that there is only ONE column for the name, and the remaining columns can be converted to floats.

浮生面具三千个 2024-10-30 20:04:04

您可以使用 [1:] 对结果列表进行切片,

for line in results:
    scores = line.split(';')[1:]
    scores = map(float, scores) # Conversion to float
    average = sum(scores)/len(scores)

这也更通用,因为您不会依赖于分数的数量,只需拒绝第一个元素:)

You can slice the result list with [1:]

for line in results:
    scores = line.split(';')[1:]
    scores = map(float, scores) # Conversion to float
    average = sum(scores)/len(scores)

that's also more general, because you won't depend on number of scores, just reject the first element :)

痴骨ら 2024-10-30 20:04:04

文件cici.csv

Johnny;8.65;   7.32;7.81;9.12 
    Juan;9.12;8.45;8.80;5.60 ;  12.455   ; 2
Joseph Maria y Borbon;8.45;9.00;9.12;9.13 
Stacey      ;7.81    ;8.10

代码

import csv

rid = csv.reader(open('cici.csv','rb'),delimiter=(';'))
av = dict( (row[0].strip(),sum(map(float,row[1:]))/(len(row)-1)) for row in rid)
print av

结果

{'Joseph Maria y Borbon': 8.925, 'Juan': 7.7375, 'Stacey': 7.955, 'Johnny': 8.225}

file cici.csv

Johnny;8.65;   7.32;7.81;9.12 
    Juan;9.12;8.45;8.80;5.60 ;  12.455   ; 2
Joseph Maria y Borbon;8.45;9.00;9.12;9.13 
Stacey      ;7.81    ;8.10

code

import csv

rid = csv.reader(open('cici.csv','rb'),delimiter=(';'))
av = dict( (row[0].strip(),sum(map(float,row[1:]))/(len(row)-1)) for row in rid)
print av

result

{'Joseph Maria y Borbon': 8.925, 'Juan': 7.7375, 'Stacey': 7.955, 'Johnny': 8.225}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文