Reading file string into an array (In a pythonic way)

后端 未结 5 797
礼貌的吻别
礼貌的吻别 2020-12-06 03:39

I\'m reading lines from a file to then work with them. Each line is composed solely by float numbers.

I have pretty much everything sorted up to convert the lines in

5条回答
  •  执笔经年
    2020-12-06 04:20

    One possible one-liner:

    a_list = [map(float, line.split(' ')) for line in a_file]
    

    Note that I used map() here instead of a nested list comprehension to aid readability.

    If you want a numpy array:

    an_array = np.array([map(float, line.split(' ')) for line in a_file])
    

提交回复
热议问题