-2

There are some data type is float, for example

24.0, 34.0, 35.6, 33.2, 44.0

I want

24.0, 34.0, 44.0

change into

24,34,44.

35.6 and 33.2 do not change.

how could I do that?

NASSER
  • 5,900
  • 7
  • 38
  • 57
jianbing Ma
  • 355
  • 1
  • 3
  • 15

3 Answers3

2

Let me put that data types into a list,

 list_num = [24.0, 34.0, 35.6, 33.2, 44.0]
 newList=[int(i) if int(i)== i else i for i in list_num]
 print newList
Sujay Narayanan
  • 487
  • 4
  • 11
1

Since 44.0 == 44 #True you can do the following:

li = [24.0, 34.0, 35.6, 33.2, 44.0]
print map(lambda x: int(x) if int(x) == x else x, li)
>> [24, 34, 35.6, 33.2, 44]
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

using string formatting:

>>> l = [24.0, 34.0, 35.6, 33.2, 44.0]
>>> ['{0:g}'.format(x) for x in l]
['24', '34', '35.6', '33.2', '44']
Gryphius
  • 75,626
  • 6
  • 48
  • 54