473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to parse a list of decimal numbers in python...

1
In an effort to learn how to work with information in files, I am writing a program that opens an already existing file containing decimal numbers. It then takes the decimals, sorts them, and returns median and mean.

Here's what I have so far:

Expand|Select|Wrap|Line Numbers
  1. #opening file as a read only file
  2. f = open('testfile', 'r') 
  3.  
  4. #assigns variable to string
  5. numbers = f.read(testfile) 
  6.  
  7. #Make a list where each decimal is its own string 
  8.  
  9. numbers.split(",")
  10.  
So...I have the list part accomplished. string.split(sep) yields the information as a list. But I don't know how to take these data from the list and make each decimal number it's own string.

I am still a bit new to python, so maybe I am going about this the wrong way...or maybe it's obvious?
May 12 '10 #1
1 3986
Glenton
391 Expert 256MB
Hi. You're doing it right so far.

You need to convert the string to a float, with the float command. This can be done by looping over the string, or more efficiently with a so-called comprehension.

Hopefully the following interactive session will help you:
Expand|Select|Wrap|Line Numbers
  1. In [15]: num=["1","2","3.45"]
  2.  
  3. In [16]: type(num[0])
  4. Out[16]: <type 'str'>
  5.  
  6. In [17]: num2=[float(item) for item in num]
  7.  
  8. In [18]: num2
  9. Out[18]: [1.0, 2.0, 3.4500000000000002]
  10.  
  11. In [19]: type(num2)
  12. Out[19]: <type 'list'>
  13.  
  14. In [20]: type(num2[0])
  15. Out[20]: <type 'float'>
  16.  
Post back to let us know how it goes.
May 13 '10 #2

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

Similar topics

5
by: Mattias Brändström | last post by:
Hello! I am trying to find a minimal class/lib that handles arbitrary precision decimal numbers. I would be happy if this class supported as little as addition, subtraction, multiplication,...
5
by: | last post by:
Ok, I know this sounds bizarre and I regret to say it has me baffled. New decimal fields in tables will accept entry of decimal numbers but immediately change same into integers. Can someone...
3
by: Gustaf Liljegren | last post by:
I searched for previous answers on this, but couldn't find something fitting. I need advice on how to store decimal numbers with possible null values in memory. The numbers may be negative, so...
6
by: Stropher | last post by:
Hi! I just want to update a column in a table having a value of a decimal number. Then I tried this via sql-query analyzer update tablepost set preis = 0.71 where idnumber = 50 It worked...
13
by: Girish Sahani | last post by:
Hi, I want to truncate every number to 2 digits after the decimal point. I tried the following but it doesnt work. >>> a = 2 >>> b = 3 >>> round(a*1.0 / b,2) 0.67000000000000004
9
by: jm.suresh | last post by:
Hi, I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. But I do not want to use eval() because I do not want to execute any code in that string and limit...
3
by: DustWolf | last post by:
Hello, I am wondering, what is the standard for including decimal numbers in XML code? What determines what is the decimal delimiter and what can be the grouping symbol? I have just realized...
1
by: Twanne | last post by:
Hi, I've got some code in VBA where I calculate some numbers. Now some of them are large decimal numbers like -6,3216324E03. So in scientific notations. Now I need to add them to a table with an...
10
by: mott3510 | last post by:
I am new with python and programming in general so I am sorry if what I am about to say doesn't make any sense. I have several different lists of numbers in excel and I would like to save each...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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,...

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.