-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,730
  • 7
  • 37
  • 56
jianbing Ma
  • 295
  • 1
  • 3
  • 14

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
  • 72,713
  • 11
  • 96
  • 140
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
  • 71,655
  • 6
  • 46
  • 52