简体   繁体   English

如何将浮点数转换为列表中的整数

[英]How to transform floats to integers in a list

I have the code below which takes a list of coefficients which are floats and I want to turn the ones that are actually integers, to integers (for example 2.0 I want to become just 2). 我下面的代码包含一个浮点系数列表,我想将实际上是整数的那些值转换为整数(例如2.0,我想变成2)。

So I have the code below and the output of the 3 print functions are: [ 0. 0. -0. 0. -0. -0. -0. 0. 0.5 0. ] 因此,我有下面的代码,这3个打印功能的输出是: [ 0. 0. -0. 0. -0. -0. -0. 0. 0.5 0. ] [ 0. 0. -0. 0. -0. -0. -0. 0. 0.5 0. ] [ 0. 0. -0. 0. -0. -0. -0. 0. 0.5 0. ] , [ 0. 0. -0. 0. -0. -0. -0. 0. 0.5 0. ]

0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0

and [ 0. 0. 0. 0. 0. 0. 0. 0. 0.5 0. ] . [ 0. 0. 0. 0. 0. 0. 0. 0. 0.5 0. ] So based on the printing, it enters the "if" statements, which means it confirms that my coefficients are actually integers (I have also tried "is_integer" function, getting the same result), but for some reason it doesn't convert them to integers. 因此,基于打印,它会输入“ if”语句,这表示它确认我的系数实际上是整数(我也尝试过“ is_integer”函数,得到相同的结果),但是由于某种原因,它不会对它们进行转换到整数。 Can somebody help me with this? 有人可以帮我吗? Thank you! 谢谢!

def function(coeffs):    
    coeffs = np.round(coeffs,2)
    print(coeffs)
    for w in range(len(coeffs)):
        if coeffs[w]-int(coeffs[w])==0.0:
            coeffs[w] = int(coeffs[w])
            print(coeffs[w])
    print(coeffs)

Your data looks like it is probably coming in as a numpy array, with a float dtype. 您的数据看起来可能以numpy数组的形式传入,并带有float dtype。 You are assigning integers back into this numpy float array. 您正在将整数分配回此numpy浮点数组。 This will not change the type of individual entries, as numpy arrays have just one type for the all entries in the array. 这不会改变单个条目的类型,因为numpy数组对于数组中的所有条目只有一种类型。

You could produce a new Python list with the results in. 您可以生成一个包含结果的新Python列表。

Something like this works: 像这样的作品:

result = [ int(x) if x-round(x)<0.0001 else x for x in np.array([1.1, 2.0, 3]) ]

You can add a type-checking condition in the loop, such as if int(coeffs[w]) == coeffs[w] , because I suspect it will be more robust as a solution. 您可以在循环中添加类型检查条件,例如if int(coeffs[w]) == coeffs[w] ,因为我怀疑它作为解决方案会更可靠。

Also, I'd suggest creating a new list or using a lambda, as modifying a list (or in this case, a numpy array, which has a set data type) at the same time as you're accessing it, generally poses traps. 另外,建议您在访问列表的同时创建一个新列表或使用一个lambda,因为在访问列表的同时修改列表(或者在这种情况下,是一个numpy数组,该数组具有设置的数据类型) 。

The following should work nicely. 以下应该很好地工作。

new_coeffs = []
def function(coeffs):    
    coeffs = np.round(coeffs,2)
    print(coeffs)
    for w in range(len(coeffs)):
        if int(coeffs[w]) == coeffs[w]:
            new_coeffs.append(int(coeffs[w]))
        else:
            new_coeffs.append(coeffs[w])
    print(new_coeffs)

This produces the following result, which I suspect is what you require. 这将产生以下结果,我怀疑这是您所需要的。

>>> new_coeffs = []
>>> a = [1.0, 2.0, 4.1, -8.0, -1.3]
>>> function(a)
[ 1.   2.   4.1 -8.  -1.3]
[1, 2, 4.1, -8, -1.3]

One way to do this is to check if both ceil and floor functions return the same value. 一种方法是检查ceilfloor函数是否都返回相同的值。 For such floats should be same. 对于这样的浮子应该是相同的。

import math
...
for w in range(len(coeffs)):
    if math.floor(coeffs[w]) == math.ceil(coeffs[w]):
        print(int(coeffs[w]))

This would be a great place to use list comprehension. 这将是使用列表理解的好地方。 I am not quite sure why the code you've given does not work, but I can give you the code which would make it work. 我不太确定为什么您给出的代码不起作用,但是我可以给您提供使之起作用的代码。

coeffs = [int(x) if x.is_integer() else x for x in coeffs]

This line goes through your coeffs array and for every x, it checks if it is an integer with the is_integer() function you mentioned. 这行代码通过您的coeffs数组,对于每个x,使用您提到的is_integer()函数检查它是否为整数。 If it is, int(x) is added, else the float itself is added. 如果是,则添加int(x) ,否则添加浮点数本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM