将字符串存储为数字列表

时间:2016-10-29 22:43:22

标签: python list python-2.7 parsing

我在python中寻找能够转换字符串的方法,该字符串可能(不同长度)看起来像这样。该字符串包含从100个不同文件中的特殊文件中提取的数据点。

 [ 52.61236 -3.144785 -11.27863 -7.346569 11.27105 -2.408065 -10.35697 -15.61926 -2.353437 3.109831 -9.151857 -18.2364 -10.63264 
  56.55116 -9.186506 -13.75657 5.94078 5.96905 -1.483013 -4.259864 -12.57798 -0.9668677 -10.42454 13.35543 -9.19768 -14.42702 
  56.55116 -12.68435 -22.5432 12.2574 -1.100278 1.703274 6.538071 -3.648291 -1.31351 -6.617892 7.883823 1.777809 -20.30247 
  58.31721 -15.8642 -29.1799 -2.507436 9.061881 -8.988363 -2.703156 -9.803705 3.01952 -5.810421 -11.41331 6.092502 -14.42702 
  54.18788 -21.26995 -13.06826 -1.524487 7.294549 -6.622187 -6.594927 -7.723001 -0.4469042 -10.07848 1.881792 4.235661 2.776365 
  51.8246 -24.13182 1.875062 -0.08787012 -0.6584454 -6.10827 -5.686847 -17.57512 -2.70008 -7.425363 3.382299 2.605522 1.001098 
  47.8858 -16.81816 -5.772095 -7.346569 -0.2166124 -9.863102 -7.11383 -10.75736 -2.006795 -5.233656 2.45891 0.7017822 0.5999346 ]

到一个数字列表。我在python中这样做,所以我想有一个方便的库我可以使用的地方?但是哪一个?

1 个答案:

答案 0 :(得分:3)

重要的是要知道这种相当奇怪的字符串来自哪里,但如果这是你拥有的东西而你无法改变它,这里有几个不同的方法。

选项1

通过用逗号替换数字之间的空格来预处理字符串,并使用ast.literal_eval()加载到Python列表中:

In [1]: s = """ [ 52.61236 -3.144785 -11.27863 -7.346569 11.27105 -2.408065 -10.35697 -15.61926 -2.353437 3.109831 -
   ...: 9.151857 -18.2364 -10.63264 
   ...:   56.55116 -9.186506 -13.75657 5.94078 5.96905 -1.483013 -4.259864 -12.57798 -0.9668677 -10.42454 13.35543 -
   ...: 9.19768 -14.42702 
   ...:   56.55116 -12.68435 -22.5432 12.2574 -1.100278 1.703274 6.538071 -3.648291 -1.31351 -6.617892 7.883823 1.77
   ...: 7809 -20.30247 
   ...:   58.31721 -15.8642 -29.1799 -2.507436 9.061881 -8.988363 -2.703156 -9.803705 3.01952 -5.810421 -11.41331 6.
   ...: 092502 -14.42702 
   ...:   54.18788 -21.26995 -13.06826 -1.524487 7.294549 -6.622187 -6.594927 -7.723001 -0.4469042 -10.07848 1.88179
   ...: 2 4.235661 2.776365 
   ...:   51.8246 -24.13182 1.875062 -0.08787012 -0.6584454 -6.10827 -5.686847 -17.57512 -2.70008 -7.425363 3.382299
   ...:  2.605522 1.001098 
   ...:   47.8858 -16.81816 -5.772095 -7.346569 -0.2166124 -9.863102 -7.11383 -10.75736 -2.006795 -5.233656 2.45891 
   ...: 0.7017822 0.5999346 ]"""

In [2]: from ast import literal_eval

In [3]: import re

In [4]: l = literal_eval(re.sub(r"(?<=\d)\s+(?=[\-\d])", ",", s.strip()))

In [5]: l
Out[5]: 
[52.61236,
 -3.144785,
 -11.27863,
 ...
 2.45891,
 0.7017822,
 0.5999346]

选项2(可能是最简单的)

strip()方括号,split()按空格并使用float()将各个子字符串转换为浮点数:

In [13]: [float(item) for item in s.strip(" []").split()]
Out[13]: 
[52.61236,
 -3.144785,
 -11.27863,
 ...
2.45891,
 0.7017822,
 0.5999346]

选项3

使用re.findall()查找所有带有破折号,数字和点的子字符串,然后使用“map”float函数获取浮点数列表(在Python 2中):

In [14]: map(float, re.findall(r"[\d\-.]+", s))
Out[14]: 
[52.61236,
 -3.144785,
 -11.27863,
...
 2.45891,
 0.7017822,
 0.5999346]