python中if-else循环的帮助
嗨,这是我的问题。我有一个程序可以计算列中数据的平均值。 范例 输出是python中if-else循环的帮助,python,Python,嗨,这是我的问题。我有一个程序可以计算列中数据的平均值。 范例 输出是 Bob 2 部分数据为“不适用” 乔也是这样 Joe NA NA NA 我希望此输出为NA 所以我写了一个if-else循环 问题是它不执行循环的第二部分,只打印一个NA。有什么建议吗 这是我的节目: with open('C://achip.txt', "rtU") as f: columns = f.readline().strip().split(" ") numRows = 0 sums
Bob
2
部分数据为“不适用”
乔也是这样
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
好吧,我试过罗杰的建议,现在我有一个错误:
回溯(最近一次呼叫最后一次):
文件“C:/avy14.py”,第5行,在
对于f中的行:
ValueError:对关闭的文件执行I/O操作
以下是新代码:
以open('C://achip.txt',“rtU”)作为f:
columns=f.readline().strip().split(“”)
总和=[0]*len(列)
行数=0
对于f中的行:
line=line.strip()
如果不是直线:
继续
行数+=1
对于列,枚举(line.split())中的v:
如果总和[col]不是无:
如果v==“NA”:
总和[列]=无
其他:
和[col]+=int(v)
打开(“c:/chipdone.txt”,“w”)作为输出:
对于名称,zip中的总和(列,总和):
打印出>>名称,
如果总和为零:
打印>>输出“NA”
其他:
打印>>输出,求和/行
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
乔1.75
鲍勃2.33333
萨姆娜
此格式为3行(ColumnName,Avg)列,但如果需要,您可以更改输出。当然:
在获取列名时,我还将使用split的无参数版本(它允许有多个空格分隔符)
关于您的编辑以包括输入/输出示例,我保留了您的原始格式,我的输出将是:
Joe 1.75
Bob 2.33333333333
Sam NA
乔1.75
鲍勃2.33333
萨姆娜
此格式为3行(ColumnName,Avg)列,但如果需要,您可以更改输出。当然: 使用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 --]
使用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 --]
使用您的原始代码,我将添加一个循环并编辑print语句
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]
此解决方案还以Roger的格式提供相同的结果,而不是您预期的格式。使用您的原始代码,我将添加一个循环并编辑打印语句
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]
此解决方案还以Roger的格式提供相同的结果,而不是您预期的格式。下面的解决方案更简洁,代码行更少
import pandas as pd
# read the file into a DataFrame using read_csv
df = pd.read_csv('C://achip.txt', sep="\s+")
# compute the average of each column
avg = df.mean()
# save computed average to output file
avg.to_csv("c:/chipdone.txt")
这种解决方案简单性的关键在于将输入文本文件读入数据帧的方式。Pandas read_csv允许您使用正则表达式指定sep/分隔符参数。在本例中,我们使用“\s+”正则表达式模式来处理列之间的一个或多个空格
一旦数据进入数据帧,计算平均值并保存到文件都可以通过直接的函数完成。下面的解决方案更干净,代码行更少
import pandas as pd
# read the file into a DataFrame using read_csv
df = pd.read_csv('C://achip.txt', sep="\s+")
# compute the average of each column
avg = df.mean()
# save computed average to output file
avg.to_csv("c:/chipdone.txt")
这种解决方案简单性的关键在于将输入文本文件读入数据帧的方式。Pandas read_csv允许您使用正则表达式指定sep/分隔符参数。在本例中,我们使用“\s+”正则表达式模式来处理列之间的一个或多个空格
一旦数据进入数据帧,计算平均值并保存到文件中都可以使用直接的函数。使用“C:\\file”或“C:/file”,后者通常是首选;在许多情况下,使用“/”会被错误地解释(只是在这个情况下不正确)。您能粘贴一个源文件的示例,以及完整输出的示例吗?…并且,您能包含“循环的第二部分”的代码吗?提供的代码仅包含两个可选指令(如果/否则)…使用“C:\\file”或“C:/file”,通常首选后者;在许多情况下,使用“/”会被错误地解释(只是在这个情况下不正确)。您能粘贴一个源文件的示例,以及完整输出的示例吗?…并且,您能包含“循环的第二部分”的代码吗?提供的代码仅包含两条可选指令(if/else)…@Robert:您在编辑中包含的代码与with之外的for循环错误插入,在for循环运行之前关闭文件。更新了我的代码以显示我的意思。@Robert:我还发现我写的代码(在你加入示例之前)是错误的,因为我误解了你。修好了,还是不工作,罗杰。现在,当我有一个像Joe2Na1这样的名字时……最终值应该是1.5,它输出为NA@Robert:使用0.0而不是0表示和(因此使用浮点),我得到Joe 1.75,Bob 2.333..,Sam NA表示您在问题中给出的输入示例。这些值与我手工计算出来的值相匹配。@Robert:您在编辑中包含的代码错误地嵌入了with之外的for循环,在for循环运行之前关闭了文件。更新了我的代码以显示我的意思。@Robert:我还发现我写的代码(在你加入示例之前)是错误的,因为我误解了你。修好了,还是不工作,罗杰。现在,当我有一个像Joe2Na1这样的名字时……最终值应该是1.5,它输出为NA@Robert:使用0.0而不是0表示和(因此使用浮点),我得到Joe 1.75,Bob 2.333..,Sam NA表示您在问题中给出的输入示例。这些值与我手工计算的值相匹配。