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
#1
Hello!

I'm writing to ask how to round off the given list l = [0.0, 1.23, 1.68] so it reads as l = [0, 1, 1.5], with also having an option to round it off as l = [0, 1, 2].

Thank you!
Reply
#2
Sounds like homework, so thread moved.
Also, please show what you have tried.
Thank You
Reply
#3
Thank you for your reply. This is not homework. I'm working on a larger project in which I'm testing a transformation of strings.

My apology for not being able to provide more information. I'm somewhat familiar with Python's round() function, and while I have found some additional information on other Python forums, I don't know how to use it to produce what I described in my earlier post.

Any help would be appreciated. Thank you once again!
Reply
#4
Homework or not, you need to give it a try and show us your best attempt.
Reply
#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
#6
Never use list as a name for your lists. It overwrites python list.
def streched(anylist):
    list_strech = [x * 1.4 for x in anylist]
    list_cleaned = [float(round(x * 2) / 2) for x in list_strech]
    print('list_strech: {}'.format(list_strech))
    print('list_cleaned: {}'.format(list_cleaned))
 
zlist = [0, 6, 11, 16]
streched(zlist)
output
Output:
list_strech: [0.0, 8.399999999999999, 15.399999999999999, 22.4] list_cleaned: [0.0, 8.5, 15.5, 22.5]
Reply
#7
Thank you very much for your help and advice!

When I try to remove the trailing zeros using this code:
list_cleaned = [str(s).rstrip('0').rstrip('.') for s in list_round]

I get this output:
list_cleaned: ['0', '8.5', '15.5', '22.5']

Would you be willing to advise on how to remove quotes so that the output reads:
list_cleaned: [0, 8.5, 15.5, 22.5]

Thank you!
Reply
#8
Examine post 6 more closely, look at the output.
You don't need the list_cleaned line, I did it all on line 3.
The result is what you're asking for: list_cleaned: [0.0, 8.5, 15.5, 22.5]
Reply
#9
I understand. I should have asked if there's a way to modify the line 3 in post 6 so that the trailing decimal zero (.0) is removed from list_cleaned output. While the output in the revised code (post 6) is providing the desired result, I still need the zeros to be integers, like this:

list_cleaned = [0, 8.5, 15.5, 22.5]

I was able to achieve this using rstrip() function (see post 7), but can't get rid of the resulting quotes. The eventual application of this code will be in constructing the musical structure, namely chords and pitch collections, where for it to be used 0s must be represented as integers, and a string of numbers must be "flat" without quotes.

Thank you once again for your guidance. I appreciate your time and patience.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to round this code without getting an error? wlsa 2 2,421 Jun-30-2018, 07:57 PM
Last Post: ljmetzger
  def functions and floats alwillia 3 4,415 May-26-2018, 07:31 PM
Last Post: buran
  I need help with floats and int calloflegend11 4 3,787 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