Posts: 13
Threads: 5
Joined: Nov 2018
Mar-30-2019, 01:37 PM
(This post was last modified: Apr-01-2019, 01:11 AM by nagymusic.)
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!
Posts: 11,872
Threads: 474
Joined: Sep 2016
Sounds like homework, so thread moved.
Also, please show what you have tried.
Thank You
Posts: 13
Threads: 5
Joined: Nov 2018
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!
Posts: 2,344
Threads: 62
Joined: Sep 2016
Homework or not, you need to give it a try and show us your best attempt.
Posts: 13
Threads: 5
Joined: Nov 2018
Mar-31-2019, 05:59 PM
(This post was last modified: Mar-31-2019, 07:48 PM by Larz60+.)
Thank you for your patience... and encouragement ! 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)
Posts: 11,872
Threads: 474
Joined: Sep 2016
Mar-31-2019, 07:58 PM
(This post was last modified: Mar-31-2019, 07:58 PM by Larz60+.)
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]
Posts: 13
Threads: 5
Joined: Nov 2018
Apr-01-2019, 01:07 AM
(This post was last modified: Apr-01-2019, 01:07 AM by nagymusic.)
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!
Posts: 11,872
Threads: 474
Joined: Sep 2016
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]
Posts: 13
Threads: 5
Joined: Nov 2018
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.
Posts: 3,458
Threads: 101
Joined: Sep 2016
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]
|