2

my code produces this result

[['1.2', ' 4.3', ' 7', '0'], ['3', ' 5', ' 8.2', '9'], ['4', ' 3', ' 8', '5.6'], ['8', ' 4', ' 3', '7.4']]

but i want to remove the ' '

def main():
    my_list = [line.strip().split(',') for line in open("Alpha.txt")]
    print(my_list)


main()

i attempted to convert the list into floats but it keeps returning errors. I need a way to convert the current list in this format into floats.

[float(i) for i in lst]

this hasnt worked for me because it seems to error out when trying to use float(my_list)

user2840144
  • 173
  • 1
  • 3
  • 10

4 Answers4

3

You want [[float(i) for i in j] for j in lst] instead of [float(i) for i in j], the list is nested - you have a list of list of floats, not just a list of floats like you would need for your code to work. Also, it would be better to open the file with a with statement, and you might want to use a try... except... to catch exceptions in case some of the numbers in the file can't be turned into floats - eg. if one line is 1, 2, 34, 56thiswillcrashyourprogramatthemoment, 7, 8, 9.

Community
  • 1
  • 1
rlms
  • 10,650
  • 8
  • 44
  • 61
  • AHHH! thanks so much, i shouldve know it would be nested. Thanks again – user2840144 Oct 23 '13 at 15:36
  • @user2840144 if this answer helped you it would be good to accept it. – rlms Oct 23 '13 at 15:42
  • 2
    Better would be to do it all in one step `my_list = [map(float, line.split(',')) for line in open("Alpha.txt")]` or `my_list = [[float(i) for i in line.split(',')] for line in open("Alpha.txt")]` – Steven Rumbalski Oct 23 '13 at 16:16
  • i like the combination a lot steven, Cheers! – user2840144 Oct 23 '13 at 17:06
  • @StevenRumbalski This is better *iff* the OP is on CPython. If they are using another python implementation then there is no guarantee that the file will be closed after the comprehension, and in that case `with` is a better method. See http://stackoverflow.com/a/7396043/1399279 – SethMMorton Oct 23 '13 at 18:14
0

In your case, i is gonna be ['1.2', ' 4.3', ' 7', '0'] and obviously, you can't float a list!

Well, you'll have to use a double iteration:

[[float i for i in j] for j in my_list]

Hope this helps!!

aIKid
  • 26,968
  • 4
  • 39
  • 65
  • 1
    If the OP wants to maintain the same data structure (i.e. a list of lists) then this will not work because this returns all the numbers as one long list. – SethMMorton Oct 23 '13 at 15:37
0

You can use map to cast those strings in floats

my_list = map(lambda item: ( map ( lambda s: float(s), item ) ), my_list)

You will get some fancy numbers due to python precision

>>> map(lambda item: (map(lambda s: float(s), item)), l)

[[1.2, 4.2999999999999998, 7.0, 0.0], [3.0, 5.0, 8.1999999999999993, 9.0], [4.0, 3.0, 8.0, 5.5999999999999996], [8.0, 4.0, 3.0, 7.4000000000000004]]

aimnor
  • 91
  • 4
  • 3
    It isn't Python's precision, it is the underlying architecture of the computer. – rlms Oct 23 '13 at 15:43
  • 2
    I'm not sure this is easier to read than `[[float(i) for i in j] for j in my_list]`. Also, in python 3.x, `map` returns a map object, not a list, so to turn this into a list you would need to wrap the whole thing in `list`. – SethMMorton Oct 23 '13 at 15:44
  • 1
    Also, `map` looks ugly and unpythonic there, better to use a list comprehension IMHO. – rlms Oct 23 '13 at 15:44
  • Yep, the optimization/human-readable ratio is not favorable for map – aimnor Oct 23 '13 at 15:54
  • 1
    You could do `map(float, item)` rather than `map(lambda s: float(s), item)`, which seems much better to me. – rlms Oct 23 '13 at 16:09
  • Also, with regards to optimization, I don't think `map` is much faster than a list comprehension, indeed when I did a quick test to check I found it was almost twice as slow. – rlms Oct 23 '13 at 16:10
0

If you are planning on doing calculations with your data, you might consider numpy and import using numpy.genfromtxt:

import numpy as np
my_array = np.genfromtxt('Alpha.txt',delimiter=',')

If required, this could be converted to a list like so:

my_list = my_array.tolist()
Lee
  • 29,398
  • 28
  • 117
  • 170
  • Too complicated. And if he doesn't have numpy? – aIKid Oct 23 '13 at 15:50
  • It is not complicated. Numpy is available in a [number of distributions](http://www.scipy.org/install.html). Depending on the user's application, it might be preferable to using lists (and therefore is a useful option). – Lee Oct 23 '13 at 15:58