How to Transform Floats to Integers in a List

Convert list values from float to int in python

You can use list-comprehension, each element inside the list is tuple, so you need to take them into account individually, also you can use int() instead of round() to convert floating point number to integer:

[(int(x), int(y)) for x,y in lst]

[(22027, 22943), (22026, 22939), (22025, 22936), (22025, 22932), (22027, 22929), (22030, 22926), (22031, 22922), (22033, 22919), (22033, 22907), (22030, 22908), (22029, 22911), (22027, 22914), (22025, 22918), (22021, 22930), (22018, 22931), (22015, 22928), (22012, 22924), (22011, 22921), (22011, 22920)]

Python: Convert Float to Int in List

Please try to provide full error traceback documentation in future posts to help people better assist you.

It's fundamentally impossible to concatenate type int to a str initialization unless you cast the int as a str (i.e., the opposite of what you're trying to do). I'm not really sure what your end goal is here, but I can assure you that the problem is not that the strings which you are trying to cast as integers are not being converted (in the case where they are numbers in string form), it's rather just that you can't concatenate them to an empty string.

If there's something else you're trying to do here, I can try to help if you can elaborate.

Converting float to integers in a specific sublist

You can use list comprehension for the first list (that needs transformation), and add the second list (which you don't need to change).

print([[int(aa) for aa in a[0]], a[1]])

Out[46]: [[1, 2], [6, 8]]

Unlike the other solutions, this does not change the 2nd part of the list in any way.

How to convert float to int in a list in python?

Some of your values are NaN. Also, you missed the assignment operation.

import math
for i in range(len(d)):
if not math.isnan(d[i]['age']):
d[i]['age'] = int(d[i]['age'])

Convert/Cast float list to int list

You can try using List.map together with int_of_float to convert floats to integers.

Example:

let float_list = [1.0; 2.0; 3.0] in
let int_list = List.map (fun x -> int_of_float x) float_list in
(* int_list is [1; 2; 3] *)
...

How to convert float list to int list in python

The problem seems to be that

maxx=sorted(int_a,reverse=True)[:1]*1.2
print maxx

... produces a list, not an integer and you cannot multiply a list by a floating point number. To obtain 1.2 times the maximum element in the list using this code, the following would work:

maxx=sorted(int_a,reverse=True)[0]*1.2
print maxx

... though it would be more efficient to use:

maxx=max(int_a)*1.2
print maxx


Related Topics



Leave a reply



Submit