Python Forum
Round off floats in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Round off floats in a list
#10
Sounds like you want some of the values to be floats, and some of them to be ints. Have you tried just converting them?
>>> items = [0.0, 8.5, 15.5, 22.5]
>>> list(item if item else int(item) for item in items)
[0, 8.5, 15.5, 22.5]
Reply


Messages In This Thread
Round off floats in a list - by nagymusic - Mar-30-2019, 01:37 PM
RE: Round off floats in a list - by Larz60+ - Mar-30-2019, 02:26 PM
RE: Round off floats in a list - by nagymusic - Mar-30-2019, 02:49 PM
RE: Round off floats in a list - by micseydel - Mar-30-2019, 07:04 PM
RE: Round off floats in a list - by nagymusic - Mar-31-2019, 05:59 PM
RE: Round off floats in a list - by Larz60+ - Mar-31-2019, 07:58 PM
RE: Round off floats in a list - by nagymusic - Apr-01-2019, 01:07 AM
RE: Round off floats in a list - by Larz60+ - Apr-01-2019, 01:45 AM
RE: Round off floats in a list - by nagymusic - Apr-01-2019, 12:05 PM
RE: Round off floats in a list - by nilamo - Apr-01-2019, 07:43 PM
RE: Round off floats in a list - by nagymusic - Apr-02-2019, 12:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to round this code without getting an error? wlsa 2 2,466 Jun-30-2018, 07:57 PM
Last Post: ljmetzger
  def functions and floats alwillia 3 4,500 May-26-2018, 07:31 PM
Last Post: buran
  I need help with floats and int calloflegend11 4 3,880 Aug-27-2017, 10:29 PM
Last Post: BerlingSwe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020