帮忙解决Python中的if else循环

1 投票
4 回答
1734 浏览
提问于 2025-04-16 04:33

你好,我遇到了一个问题。我有一个程序,它用来计算数据列的平均值。

Bob
1
2
3

输出结果是:

Bob
2

有些数据里有'na'这个值,所以对于Joe来说:

Joe
NA
NA
NA

我希望这个输出是NA。

为此我写了一个if else循环。

问题是它没有执行循环的第二部分,只打印出一个NA。你有什么建议吗?

这是我的程序:

with open('C://achip.txt', "rtU") as f:
    columns = f.readline().strip().split(" ")
    numRows = 0
    sums = [0] * len(columns)

    numRowsPerColumn = [0] * len(columns) # this figures out the number of columns

    for line in f:
        # Skip empty lines since I was getting that error before
        if not line.strip():
            continue

        values = line.split(" ")
        for i in xrange(len(values)):
            try: # this is the whole strings to math numbers things
                sums[i] += float(values[i])
                numRowsPerColumn[i] += 1
            except ValueError:
                continue 

    with open('c://chipdone.txt', 'w') as ouf:
        for i in xrange(len(columns)):
           if numRowsPerColumn[i] ==0 :
               print 'NA' 
           else:
               print>>ouf, columns[i], sums[i] / numRowsPerColumn[i] # this is the average calculator

文件的内容是这样的:

Joe Bob Sam
1 2 NA
2 4 NA
3 NA NA
1 1  NA

最终的输出是名字和平均值。

Joe Bob Sam 
1.5 1.5 NA

好的,我试了Roger的建议,但现在我遇到了这个错误:

追踪信息(最近的调用在前): 文件 "C:/avy14.py",第5行,在 for line in f: ValueError: 在关闭的文件上进行I/O操作

这是我新的代码:

with open('C://achip.txt', "rtU") as f: columns = f.readline().strip().split(" ") sums = [0] * len(columns) rows = 0 for line in f: line = line.strip() if not line: continue

rows += 1 for col, v in enumerate(line.split()): if sums[col] is not None: if v == "NA": sums[col] = None else: sums[col] += int(v)

with open("c:/chipdone.txt", "w") as out: for name, sum in zip(columns, sums): print >>out, name, if sum is None: print >>out, "NA" else: print >>out, sum / rows

4 个回答

0

根据你原来的代码,我会加一个循环,并修改一下打印的语句。

    with open(r'C:\achip.txt', "rtU") as f:
    columns = f.readline().strip().split(" ")
    numRows = 0
    sums = [0] * len(columns)

    numRowsPerColumn = [0] * len(columns) # this figures out the number of columns

    for line in f:
        # Skip empty lines since I was getting that error before
        if not line.strip():
            continue

        values = line.split(" ")

        ### This removes any '' elements caused by having two spaces like
        ### in the last line of your example chip file above
        for count, v in enumerate(values):      
            if v == '':     
                values.pop(count)
        ### (End of Addition)

        for i in xrange(len(values)):
            try: # this is the whole strings to math numbers things
                sums[i] += float(values[i])
                numRowsPerColumn[i] += 1
            except ValueError:
                continue 

    with open('c://chipdone.txt', 'w') as ouf:
        for i in xrange(len(columns)):
           if numRowsPerColumn[i] ==0 :
               print>>ouf, columns[i], 'NA' #Just add the extra parts
           else:
               print>>ouf, columns[i], sums[i] / numRowsPerColumn[i]

这个解决方案也会得到和罗杰的格式一样的结果,而不是你想要的格式。

0

使用numpy:

import numpy as np

with open('achip.txt') as f:
    names=f.readline().split()
    arr=np.genfromtxt(f)

print(arr)
# [[  1.   2.  NaN]
#  [  2.   4.  NaN]
#  [  3.  NaN  NaN]
#  [  1.   1.  NaN]]

print(names)
# ['Joe', 'Bob', 'Sam']

print(np.ma.mean(np.ma.masked_invalid(arr),axis=0))
# [1.75 2.33333333333 --]
1
with open("c:/achip.txt", "rU") as f:
  columns = f.readline().strip().split()
  sums = [0.0] * len(columns)
  row_counts = [0] * len(columns)

  for line in f:
    line = line.strip()
    if not line:
      continue

    for col, v in enumerate(line.split()):
      if v != "NA":
        sums[col] += int(v)
        row_counts[col] += 1

with open("c:/chipdone.txt", "w") as out:
  for name, sum, rows in zip(columns, sums, row_counts):
    print >>out, name,
    if rows == 0:
      print >>out, "NA"
    else:
      print >>out, sum / rows

我会使用不带参数的split方法来获取列名(这样可以处理多个空格作为分隔符)。

关于你添加的输入/输出示例,我保留了你原来的格式,我的输出会是:

Joe 1.75
Bob 2.33333333333
Sam NA

这个格式是3行的(列名, 平均值)列,不过如果你想的话,可以随意更改输出格式。:)

撰写回答