列长的运算 [英] operations on column length

查看:116
本文介绍了列长的运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,很抱歉,如果我使用错误的语言来解释我正在执行的操作,那么我对python还是很陌生,并且仍然不了解它.

Firstly, sorry if I have used the wrong language to explain what I'm operating on, I'm pretty new to python and still far from being knowledgeable about it.

我目前正在尝试对一列数据的长度进行操作,但是我遇到了一些问题.这些列来自第七个sdss数据转储中的.fit文件.当我运行代码时,将根据边界条件打印x1,x2,x3的每个值.

I'm currently trying to do operations on the length of a column of data however I'm running into a few problems. These columns are from a .fit file from the 7th sdss data dump. When I run the code each value for x1, x2, x3 is printed according to the boundary conditions.

x1 = o3[(03 >= y1)]
print len(x1)
x2 = o3[(o3 < y2) & (o3 < y1)]
print len(x2)
x3 = o3[(o3 < y1) & (o3 >= y2)]
print len(x3)
xtotal = len(x1) + len(x2) + len(x3)
print xtotal

从这一点上,我获得x1,x2,x3的值分别约为70000、90000和30000,总计约为190000.我想添加一行以计算这些值占总值的百分比,但是我可以做到这一点.似乎无法解决这个问题.

From this point I obtain values for x1, x2, x3 of around 70000, 90000 and 30000 with a total of around 190000. I would like to include a line to calculate what percentage these are of the total value however I can't seem to get this to work.

尝试过.

x1percentage = (xtotal/x1)*100
print x1percentage

这将返回不对长度进行运算的元素,但是当我尝试将长度定义为仅一个值时,它将返回0的答案.

This returns the elements operated on not the lengths but when i try to define the length as just a value it returns an answer of 0.

任何帮助将不胜感激.

推荐答案

您可能会在使用整数数学时出错,但是您的语句

You are probably making an error with integer math, but your statement

x1percentage = (xtotal/x1)*100

完全没有道理. xtotal是整数,x1是值的数组,len(x1)是等于x1大小的整数,并且无论要获取百分比还是要x1/xtotal.

doesn't really make sense. xtotal is an integer and x1 is an array of values, len(x1) is an integer equal to the size of x1, and regardless to get a percentage you would want x1/xtotal.

如果您尝试了len(x1)/xtotal,则您将得到0,因为python会将两个变量都转换为整数并使用整数数学,结果为零.

If you tried len(x1)/xtotal, you would get zero as you mentioned, because python would cast both variables as integers and use integer math, which the result is zero.

解决方案将一个或两个值都转换为浮点数,以强制除以浮点数

The solution cast one or both values as floats to force floating point division by

x1percentage = (xtotal / float(x1)) * 100

这篇关于列长的运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆