- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我让这段代码工作,但我想让我打印出来的部分停在小数点后的 2 个数字 (0.00) 我不知道该怎么做,任何人都可以提供一些见解我将使用什么命令来执行此操作。这是代码
import csv
#turn csv files into a list of lists
with open('train.csv','rU') as csvfile:
reader = csv.reader(csvfile)
csv_data = list(reader)
with open ('train.csv', 'r') as f:
numline = len(f.readlines())
# Create two lists to handle the patients
# And two more lists to collect the 'sum' of the columns
# The one that needs to hold the sum 'must' have 0 so we
# can work with them more easily
iList = []
iList_sum = [0,0,0,0,0,0,0,0,0,0,0,0,0]
IPcounter = 0
hList = []
hList_sum = [0,0,0,0,0,0,0,0,0,0,0,0,0]
HPcounter = 0
# Only use one loop to make the process mega faster
for row in csv_data:
# If row 13 is greater than 0, then place them as unhealthy
if (row and int(row[13]) > 0):
# This appends the whole 'line'/'row' for storing :)
# That's what you want (instead of saving only one cell at a time)
iList.append(row)
IPcounter += 1
# If it failed the initial condition (greater than 0), then row 13
# is either less than or equal to 0. That's simply the logical outcome
else:
hList.append(row)
HPcounter += 1
# Loop through all the 'rows' of the ill patient
for ill_data in iList:
# Loop through the data within each row, and sum them up
qmark_counter = 0
for i in range(0,len(ill_data) - 1):
if ill_data[i] == '?':
val = 0
else:
val = ill_data[i]
iList_sum[i] += float(val)
# Now repeat the process for healthy patient
# Loop through all the 'rows' of the healthy patient
for healthy_data in hList:
# Loop through the data within each row, and sum them up
for i in range(0,len(healthy_data) - 1):
hList_sum[i] += float(ill_data[i])
ill_avg = [ ill / len(iList) for ill in iList_sum]
hlt_avg = [ hlt / len(hList) for hlt in hList_sum]
print('Total number of lines ' + str(numline))
print("Total amount of healthy patients " + str(HPcounter))
print("Total amount of ill patients " + str(IPcounter))
print("Averages of healthy patients " + str(hlt_avg))
print("Averages of ill patients " + str(ill_avg))
这是输出
Total number of lines 303
Total amount of healthy patients 164
Total amount of ill patients 139
Averages of healthy patients [57.0, 0.0, 2.0, 130.0, 236.0, 0.0, 2.0, 174.0, 0.0, 0.0, 2.0, 1.0, 3.0]
Averages of ill patients [56.62589928057554, 0.8201438848920863, 3.5899280575539567, 134.568345323741, 251.4748201438849, 0.15827338129496402, 1.1726618705035972, 139.25899280575538, 0.5467625899280576, 1.5741007194244607, 1.8273381294964028, 1.129496402877698, 5.798561151079137]
最佳答案
您可以使用 round(number, ndigits)
其中 ndigits
是小数位数。
例如
>>> number = 32.32434354
>>> round(number, 2)
32.32
>>> round(number, 5)
32.32434
另一种方法是使用 Python 的 str.format()
.
例如
>>> '{:.2f}'.format(number)
'32.32'
>>> '{:.5f}'.format(number)
'32.32434'
在哪里,
{}
是.format()
插入数字的位置。
f
指定数字将是一个float
。
.2
或.5
表示四舍五入到小数点后两位或五位。
关于python - 小数点后第二位的停止平均数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36588093/
我试图找到一种方法来限制 Haskell,以便返回只有 2 个十进制名称的数字(例如 1.24) 我的代码是: sumSquares = sum . map (^ 2) 在 Haskell 中如何做到
我在向输入框添加停靠点验证时遇到一些问题。仅允许数字的限制(第二个条件)有效,但第一个条件也可能不存在。输入框中仍然没有出现句号和小数点。 function isNumberKey(evt) {
如何处理R中的p值? 我期望非常低的 p 值,例如: 1.00E-80 我需要-log10 -log10(1.00E-80) -log10(0) 是 Inf,但 Inf 也有四舍五入的感觉。 但似乎在
我的数据库中有一个包含货币字段的表。我已经为该货币字段创建了实体并创建了 decimal 属性。当该字段的值显示在我的 MVC3 View 上时,它在小数点后有四个零 0000,如下所示:5489.0
label.text = [NSString stringWithFormat:@"%.2f",final]; 上面的语句显示变量“final”中可用的浮点值,小数点后两位。 我想显示小数位数,具体取
有没有办法在 Qt 5 中绘制带有小数点大小的文本。我正在尝试使用 QFont::setPointSizeF() 但它似乎在我尝试过的任何平台(mac/linux/windows)上都不起作用,并且点
我有一个关于如何将基数为 10 的数字转换为 IEEE 754 浮点表示的示例 Number: 45.25 (base 10) = 101101.01 (base 2) Sign: 0 Normali
我有一个包含以下值的 CSV 文件: 3271.96;274;272;1;1;0;1;0.071690;0;0;0;0;0;0;1.753130;1.75;0;1.75; 但是当我用 Excel 打开
有人可以对 here 中的这些特殊字符示例给出更好的解释吗? ?或者提供一些更清楚的例子? (x) The '(foo)' and '(bar)' in the pattern /(foo) (bar
我编写了 change() 来帮助我将字符串转换为 double。 例如:如果字符串是“5.5”,我希望数字是5.5。如果字符串是“0.0”,我希望数字是0。另一个例子:“50”到50。 现在的问题是
我正在尝试确定 python float 与零进行比较时的精确度。请注意以下几点: 1e-323 == 0 > False 1e-324 == 0 > True 我认为阈值是 324 个小数点,至少对
我想使用 .ftr 文件来快速分析数百个表。不幸的是,我在十进制和千位分隔符方面遇到了一些问题,类似于 that post ,只是 read_feather 不允许 decimal=',', thou
在 SQL Server 2008 中,我正在运行一个报告,我需要在其中划分两列(称为 Completes 和 Prescreens,整数),然后转到 Excel并得到 Prescreens 除以 C
我有一个 NSString,我想将其转换为 NSDecimalNumber。该字符串是从服务器接收的,并且始终使用 en_US 语言环境进行格式化,例如 XXX.YYY 而不是 XXX,YYY。我想创
我是一名优秀的程序员,十分优秀!