0

I am reading data from the robot's sensor. it gives this data as a list:

data:[1.0014142543, 0.4142543254, 4.5432544179]

I'd like to convert it like this format:

data:[1.00, 0.41, 4.54]

I am running it with python2.

I think this post for one number.

any ideas or suggestions, it would be highly appreciated.

Redhwan
  • 927
  • 1
  • 9
  • 24
  • use `round(float, 2)` – Rakesh Nov 21 '19 at 07:15
  • no, this is a list, I already tried with it but not works. I am using round(float, 2) with a float number. please try to implement it. – Redhwan Nov 21 '19 at 07:21
  • 1
    @Redhwan, what is wrong with the approach @Rakesh mentioned. You can try to do that for every element in list using `map()` like - `map(lambda x: round(x, 2), data)` – kuro Nov 21 '19 at 07:44
  • 1
    [round(item,2) for item in data] – redhatvicky Nov 21 '19 at 07:49
  • Why do you want this? If this is just for display, use string formatting, not `round`ing. If you're going to perform further computations with the numbers, why would you want to throw away precision? – Mark Dickinson Nov 21 '19 at 07:55
  • thank you all, this is enough for my purpose...> map(lambda x: round(x, 2), data), I don't think about it. thanks again. – Redhwan Nov 21 '19 at 08:01

1 Answers1

2

Considering data as list ,

Using round :

>> data=[1.00, 0.41, 4.54]
>> [round(item,2) for item in data]

Using Float and Format:

>> [float(format(item,".2f")) for item in data]

Output :

[1.0, 0.41, 4.54]
redhatvicky
  • 1,912
  • 9
  • 8