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
#5
Thank you for your patience... and encouragement Wink! I appreciate the opportunity to ask questions and learn on this forum.

Here's what I managed so far. While I'm able to generate correct integers, I'm still having some difficulty in creating a flat list without quotes when removing the trailing zeros (see below). Let me know if there's a more elegant way to achieve what I'm looking for. THANK YOU!

def streched(list):
    list_strech = [x * 1.4 for x in list]
    list_round = [round(x * 2) / 2 for x in list_strech]
    list_cleaned = [str(s).rstrip('0').rstrip('.') for s in list_round]
    print(list)
    print(list_strech)
    print(list_round)
    print(list_cleaned)

list = [0, 6, 11, 16]
streched(list)
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,499 Jun-30-2018, 07:57 PM
Last Post: ljmetzger
  def functions and floats alwillia 3 4,520 May-26-2018, 07:31 PM
Last Post: buran
  I need help with floats and int calloflegend11 4 3,928 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