Python Array是只读的,不能追加值

Jar*_*own 3 python arrays

我是Python的新手.以下代码在尝试将值附加到数组时导致错误.我究竟做错了什么?

import re
from array import array

freq_pattern = re.compile("Frequency of Incident[\(\)A-Za-z\s]*\.*\s*([\.0-9]*)")
col_pattern = re.compile("([-\.0-9]+)\s+([-\.0-9]+)\s+([-\.0-9]+)\s+([-\.0-9]+)\s+([-\.0-9]+)")
e_rcs = array('f')

f = open('example.4.out', 'r')

for line in f:
    print line,

    result = freq_pattern.search(line)
    if result:
        freq = float(result.group(1))

    cols = col_pattern.search(line)
    if cols:
        e_rcs.append = float(cols.group(2))

f.close()
Run Code Online (Sandbox Code Playgroud)

错误

回溯(最近一次调用最后一次):
文件"D:\ workspace\CATS Parser\cats-post.py",第31行,在e_rcs.append = float(cols.group(2))AttributeError:'list'对象属性' append'是只读属性(分配给.append)

S.L*_*ott 6

你想附加到阵列吗?

e_rcs.append( float(cols.group(2)) )
Run Code Online (Sandbox Code Playgroud)

这样做:e_rcs.append = float(cols.group(2))用浮点值替换append数组的方法e-rcs.很少你想做什么.

  • 他没有把列表称为数组.他正在使用数组模块,该模块是Python标准库的一部分. (2认同)

Bre*_*wey 6

您要分配给append()函数,而是要调用.append(float(cols.group(2))).