473,509 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I round numbers in a list?

39 New Member
I want to round a list of numbers to basically remove all the decimal places or even just convert them to integers.

So this is what I've tried and I just can't get this to work.

Expand|Select|Wrap|Line Numbers
  1. myList = [10.5,25.1,350.6,4,5,6] #declare my list
  2. for i in range(len(myList)):
  3.     myList[i] == round(i,0)  #tries to round all numbers.
  4. print myList
  5.  
  6. #other Attempt
  7. for i in range(len(myList)):
  8.     myList[i] == int(i)  #tries to make i into an integer
  9. print myList
  10.  
  11. #other attempt
  12.  
  13. for i in myList:
  14.      int(i)
  15.  
  16.  
I'm just not getting this.

Thanks
Sep 12 '12 #1
9 24563
zmbd
5,501 Recognized Expert Moderator Expert
So what error message are you getting?
So what output, if any, are you getting?
Hint: round(i,0) returns a double so if you want to round and then return the integer part only...

-z
Sep 12 '12 #2
Miguel Valenzue
39 New Member
Okay, here's the code again:
Expand|Select|Wrap|Line Numbers
  1. myList = [10.5,25.1,350.6,4,5,6]
  2.  
  3. for i in myList:
  4.     int(i)
  5.  
  6. print myList
  7.  
  8. for i in myList:
  9.     round(i)
  10.  
  11. print myList
  12.  
  13. for i in range(len(myList)):
  14.     myList[i] == round(i,0)
  15.  
  16. print myList
  17.  
Here's my output

[10.5, 25.1, 350.6, 4, 5, 6]
[10.5, 25.1, 350.6, 4, 5, 6]
[10.5, 25.1, 350.6, 4, 5, 6]

My desirec output is
[11, 25, 351, 4, 5, 6]

No errors. Just doesn't do anything.
When you say it returns a double. clueless on what that implies.
Sep 12 '12 #3
zmbd
5,501 Recognized Expert Moderator Expert
Returns a data type of "double" vs integer, long, etc..
Data types are covered in most good books and you can google on them as related to python/ruby/vba/.net etc...

My hint has the solution.

-z
Sep 12 '12 #4
Miguel Valenzue
39 New Member
I will call you Gollum from now own, master of riddles. ;)
Okay, I understand double.
So if round is returning a double, then I need to convert that into an integer.

I've tried that (at least I think) with this code.
Expand|Select|Wrap|Line Numbers
  1. for i in myList:
  2.     int((round(i)))
  3.  
  4. print myList
But on a deeper level, is my logic right? to redefine the numbers (long) in a list by just using the for loop?
Sep 12 '12 #5
bvdet
2,851 Recognized Expert Moderator Specialist
In your first example, you are using the comparison operator "==" when you should be using the assignment operator "=". Also, you should combine int() and round() to achieve the desired output.
Expand|Select|Wrap|Line Numbers
  1. >>> int(round(12.5, 0))
  2. 13
  3. >>> x == int(round(12.5, 0))
  4. False
  5. >>> x = int(round(12.5, 0))
  6. >>> x
  7. 13
  8. >>> 
A list comprehension is the easiest way.
Expand|Select|Wrap|Line Numbers
  1. >>> x = [1,2,3,11.5]
  2. >>> newlist = [int(round(n, 0)) for n in x]
  3. >>> newlist
  4. [1, 2, 3, 12]
  5. >>> 
Sep 12 '12 #6
zmbd
5,501 Recognized Expert Moderator Expert
bvdet
sigh... you let 'em off the hook. :(
-z
Sep 12 '12 #7
bvdet
2,851 Recognized Expert Moderator Specialist
Sorry about that zmbd. If he was going to "get it" he already would have.
Sep 12 '12 #8
Miguel Valenzue
39 New Member
Well, first of all, thanks both of you. I appreciate your time.
But after looking at the way I was approaching it, I want to learn a bit more here.
I got the code to work so now my vector multiplication that I was going for is complete.

But this was my thought process.

If I put
Expand|Select|Wrap|Line Numbers
  1. int(12.5,0)
  2.  
in the command line of python, it returns 13

So that was fine with me. I get that.

But this was my thinking.
If I have a list, I can do something real basic like this

Expand|Select|Wrap|Line Numbers
  1. myList =[4,3,5,6,1]
  2.  
  3. for range in (len(myList)):
  4.      i=*3
  5.  
  6.  
That returns a new list of
12,9,15,6,1

So my thought process was, why can't I just use the round or integer function within the same statement such as

Expand|Select|Wrap|Line Numbers
  1. for range in (len(myList)):
  2.      i=int(i)
  3.  
But when I do this, it returns a number like 2.000000001 or something like that.

Anyhow, that's what I don't get so as part of my learning experience, can you help me understand what is happening? I see that round returns a double like you said, but how does that double go back to being an int?

I mean, why doesn´t the following code work?

Expand|Select|Wrap|Line Numbers
  1. for range in (len(myList)):
  2.      i=int(round(i,0))
  3.  
  4.  
thanks again
Oh, and bvdet,
I didn´t know you could use list comprehension like that.
I redid all my code and cut it down by about 25% with that new found knowledge.
Sep 12 '12 #9
bvdet
2,851 Recognized Expert Moderator Specialist
In your for loop, range is used as a temporary identifier for the expression len(myList), which returns an integer. You cannot iterate on an integer. Also, you will mask the built-in function range(). I think you meant to do this:
Expand|Select|Wrap|Line Numbers
  1. >>> myList = [10.2, 12.9]
  2. >>> for i in range(len(myList)):
  3. ...     myList[i]=int(round(myList[i],0))
  4. ...     
  5. >>> myList
  6. [10, 13]
  7. >>> 
The temporary identifier i can be used as the index for getting each list element and reassigning the element at that index. Identifier i will retain the last value assigned to it. Built-in function range() returns a list.
Expand|Select|Wrap|Line Numbers
  1. >>> range(len(myList))
  2. [0, 1]
  3. >>> i
  4. 1
  5. >>> 
HTH
Sep 12 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

11
1929
by: Leon | last post by:
I have six textbox controls on my webform that allows the user to enter any numbers from 1 to 25 in any order. However, I would like to sort those numbers from least to greatest before sending them...
9
5159
by: Lance Hoffmeyer | last post by:
Is there an easy way to round numbers in an array? I have Test = and want to round so the values are print Test
1
1544
by: Richard Cranium | last post by:
Using the number 1.015 ***************************** the following script returns 1.02, which is correct var T; var S=new String(Math.round(parseFloat(1.015).toFixed(2)*100)); while...
6
9098
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
10
4621
by: Henrootje | last post by:
I am looking for a way to round down the results of a calculation in a query f.e.: in a query this calculation is performed: a/b = c 5/3 = 1,666666667
3
1430
by: Samuel | last post by:
Hi I am looking for a method that rounds numbers to a specified number of decimal places for example if I pass 2.455 it will return 2.46 Thank you, Samuel
19
21339
by: muddasirmunir | last post by:
can any body tell me how to round numbers in in exactly two decimal places i had use a function round(text1.text,2) but whenever there is zere(0) at the end it does not show it for eg for round...
3
1814
by: Aussie Rules | last post by:
Hi, Whats the easiest way to round numbers up. For example, a calculation that results in a value of 1.3 will round to 1, and a calculation that results in 1.8 will round to 2. My issue...
4
1508
by: Iruka07 | last post by:
NOTE: I am using Excel VBA. Please help me program the following problem: A positive integer n is said to be a ROUND NUMBER if the binary representation of n has as many or more zeros as ones....
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.