1

有这个文本文件:

Breathing 1:-29.9396 9:-32.0895 13:-35.6787 2:-36.8527 17:-37.3622 8:-38.0584 14:-38.5265 18:-38.7567 16:-38.7663 12:-39.1036 7:-39.5838 4:-40.653 15:-41.2627 3:-42.5983 5:-43.5966 10:-48.5747 11:-48.8823 6:-241.384
Breathing 1:-30.3629 9:-30.9582 17:-34.2767 13:-34.2944 14:-35.9349 16:-36.4528 2:-36.5194 18:-37.2092 8:-37.425 12:-37.5344 15:-38.001 3:-38.4113 7:-38.8753 5:-40.2013 4:-42.5538 10:-43.9502 11:-44.9487 6:-227.162
Breathing 1:-30.3253 9:-36.9927 8:-38.0383 4:-39.9291 13:-40.0669 2:-40.3448 18:-40.8734 14:-40.8901 16:-41.7204 12:-42.6082 7:-43.1386 17:-44.0876 3:-48.8257 15:-48.8814 5:-51.2907 11:-58.4311 10:-59.1803 6:-266.998
Breathing 15:-35.338 9:-35.8658 17:-38.6632 12:-39.154 13:-40.9085 3:-42.836 1:-43.3277 11:-43.684 2:-43.8531 8:-45.7809 18:-46.9128 16:-47.028 14:-47.6292 5:-49.7012 7:-49.8664 4:-61.5319 10:-93.743 6:-215.678
Breathing 18:-27.0995 14:-29.5464 1:-30.6396 9:-30.9543 13:-33.1009

如何将值存储在:-数组的元素中?

我知道这段代码:

for line in lines:
    items = line.split()[1:]
    items = [item.split(':')[0] for item in items]
    items = map(int,items)
    rows.append(items)

存储之前的那些:

4

4 回答 4

5

更改[0][1]第 3 行。这将从溢出而不是第一个元素中获取第二个元素。

另外,因为第二个数字不是整数,所以map(int, items)会失败。您可能想使用float. 这是一个完整的 python 脚本,您可以作为测试运行

#!/usr/bin/env python
lines = """Breathing 1:-29.9396 9:-32.0895 13:-35.6787 2:-36.8527 17:-37.3622 8:-38.0584 14:-38.5265 18:-38.7567 16:-38.7663 12:-39.10
Breathing 1:-30.3629 9:-30.9582 17:-34.2767 13:-34.2944 14:-35.9349 16:-36.4528 2:-36.5194 18:-37.2092 8:-37.425 12:-37.5344 15:-38.00
Breathing 1:-30.3253 9:-36.9927 8:-38.0383 4:-39.9291 13:-40.0669 2:-40.3448 18:-40.8734 14:-40.8901 16:-41.7204 12:-42.6082 7:-43.138
Breathing 15:-35.338 9:-35.8658 17:-38.6632 12:-39.154 13:-40.9085 3:-42.836 1:-43.3277 11:-43.684 2:-43.8531 8:-45.7809 18:-46.9128 1
Breathing 18:-27.0995 14:-29.5464 1:-30.6396 9:-30.9543 13:-33.1009""".split('\n')

rows = []

for line in lines:
    items = line.split()[1:]
    items = [item.split(':')[1] for item in items]
    items = map(float,items)
    rows.append(items)

from pprint import pprint
pprint(rows)
于 2012-06-14T09:58:46.823 回答
1
>>> import re
>>> rows = []
>>> for line in s.split('\n'):
    for item in line.split():
        m = re.match(r'\d+:-([\d\.]+)', item)
        if m:
            rows.append(m.groups()[0])


>>> rows
['29.9396', '32.0895', '35.6787', '36.8527', '37.3622', '38.0584', '38.5265', '38.7567', '38.7663', '39.1036', '39.5838', '40.653', '41.2627', '42.5983', '43.5966', '48.5747', '48.8823', '241.384', '30.3629', '30.9582', '34.2767', '34.2944', '35.9349', '36.4528', '36.5194', '37.2092', '37.425', '37.5344', '38.001', '38.4113', '38.8753', '40.2013', '42.5538', '43.9502', '44.9487', '227.162', '30.3253', '36.9927', '38.0383', '39.9291', '40.0669', '40.3448', '40.8734', '40.8901', '41.7204', '42.6082', '43.1386', '44.0876', '48.8257', '48.8814', '51.2907', '58.4311', '59.1803', '266.998', '35.338', '35.8658', '38.6632', '39.154', '40.9085', '42.836', '43.3277', '43.684', '43.8531', '45.7809', '46.9128', '47.028', '47.6292', '49.7012', '49.8664', '61.5319', '93.743', '215.678', '27.0995', '29.5464', '30.6396', '30.9543', '33.1009']
>>> 
于 2012-06-14T10:14:08.250 回答
1

使用 re 模块!

import re
import numpy as np

fobj=open.("yourFile", "r")
data=fobj.read()
fobj.close()

r=re.findall(r":-[0-9]{1,3}.[0-9]{1,5}", data)
for i in xrange(len(r)):
    r[i]=r[i].lstrip(":")
array=np.array(r).astype(float)

如果您的所有值都是负数,您可以通过删除语句:中的 there.findall和以下for循环来简化代码。输出:

array([ -29.9396,  -32.0895,  -35.6787,  -36.8527,  -37.3622,  -38.0584,
    -38.5265,  -38.7567,  -38.7663,  -39.1036,  -39.5838,  -40.653 ,
    -41.2627,  -42.5983,  -43.5966,  -48.5747,  -48.8823, -241.384 ,
    -30.3629,  -30.9582,  -34.2767,  -34.2944,  -35.9349,  -36.4528,
    -36.5194,  -37.2092,  -37.425 ,  -37.5344,  -38.001 ,  -38.4113,
    -38.8753,  -40.2013,  -42.5538,  -43.9502,  -44.9487, -227.162 ,
    -30.3253,  -36.9927,  -38.0383,  -39.9291,  -40.0669,  -40.3448,
    -40.8734,  -40.8901,  -41.7204,  -42.6082,  -43.1386,  -44.0876,
    -48.8257,  -48.8814,  -51.2907,  -58.4311,  -59.1803, -266.998 ,
    -35.338 ,  -35.8658,  -38.6632,  -39.154 ,  -40.9085,  -42.836 ,
    -43.3277,  -43.684 ,  -43.8531,  -45.7809,  -46.9128,  -47.028 ,
    -47.6292,  -49.7012,  -49.8664,  -61.5319,  -93.743 , -215.678 ,
    -27.0995,  -29.5464,  -30.6396,  -30.9543,  -33.1009])
于 2012-06-14T11:17:59.900 回答
0

如果

items = [item.split(':')[0] for item in items]

存储之前的值:,然后

items = [item.split(':')[1] for item in items]

将值存储在:.

于 2012-06-14T09:58:37.637 回答