1

python beginner here. How can I convert

list = [2.4, 4.8532, 5.43253, 55.3838]

to

list = [2, 4, 5, 5]

Thanks.

21rw
  • 1,016
  • 1
  • 12
  • 26
  • 3
    Possible duplicate of [Taking the floor of a float](http://stackoverflow.com/questions/9404967/taking-the-floor-of-a-float) – McGrady Mar 31 '17 at 01:50
  • 1
    Possible duplicate of [Safest way to convert float to integer in python?](http://stackoverflow.com/questions/3387655/safest-way-to-convert-float-to-integer-in-python) – Mayeul sgc Mar 31 '17 at 01:52

2 Answers2

3

you can just use int(), Since int() always rounds down toward 0, which is the equivalent of chopping the decimals for floats. Note: do not use list as a variable name since it's already a built-in function and type.

lst = [2.4, 4.8532, 5.43253]
lst = [int(x) for x in lst]
print(lst)

Output:

[2, 4, 5]
Taku
  • 31,927
  • 11
  • 74
  • 85
  • 3
    `int()` **does not** round, if you want rounding, use `round()` instead – Taku Mar 31 '17 at 01:48
  • `int()` doesn't round down; it rounds towards 0 ("chopping the decimals," as you say). – TigerhawkT3 Mar 31 '17 at 01:52
  • 1
    This assumes values < 10. – chepner Mar 31 '17 at 01:52
  • Thanks. What if the values are greater than 10? – 21rw Mar 31 '17 at 02:44
  • @Umuko so do you mean you want to "take the first digit" instead of "take the first number"? What is your desired result for `24.8532`? `2` or `24` or `25`? – Taku Mar 31 '17 at 02:47
  • @abccd in this case I am looking for a 2. My examples were bad. Sorry about that. – 21rw Mar 31 '17 at 02:51
  • And one more question, will your number have negative numbers as well? – Taku Mar 31 '17 at 02:52
  • @abccd no there won't be any negatives numbers – 21rw Mar 31 '17 at 02:54
  • if it does have negative numbers: `lst = [int(str(x)[0]) if str(x)[0] != '-' else int(str(x)[1]) for x in lst]` if it doesn't, it will be easy: `lst = [int(str(x)[0]) for x in lst]`. The first way works for a list that could include both negative and non-negative numbers – Taku Mar 31 '17 at 02:56
  • 1
    `int(str(x)[0]) if str(x)[0] != '-' else int(str(x)[1])` can be more concisely expressed as `int(str(x)[x<0])`. Or you could just do `int(str(abs(x))[0])`. – TigerhawkT3 Mar 31 '17 at 03:01
  • This does not answer the question – Théophile Pace Feb 02 '19 at 17:42
  • @ThéophilePace it solved the OP’s problem, if you have a similar question and this solution does not work for you, you should ask a new question or search for a different solution. – Taku Feb 02 '19 at 19:59
2

You will have to create a new list, such as with a comprehension. You might want to get the first digit of each element, or possibly get the floor value of each element:

>>> l = [2.4, 4.8532, 5.43253]
>>> l2 = [12.4, 374.8532, -505.43253]
>>> list(map(int, (i[0] for i in map(str, l))))
[2, 4, 5]
>>> list(map(int, l))
[2, 4, 5]
>>> list(map(int, (i[0] for i in map(str, l2))))
>>> [i[0] for i in map(str, l2)]
['1', '3', '-']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '-'
>>> list(map(int, l2))
[12, 374, -505]

Note how you will need to be more specific regarding the assumptions about the contents of the list. Can the numbers be greater than 10? Can they be negative? Do you want the first digit of a negative number, or the first character? If it's always 0 < number < 10, you have nothing to worry about.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97