Python Array 是只读的,不能附加值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1651430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Array is read-only, can't append values
提问by Jared Brown
I am new to Python. The following code is causing an error when it attempts to append values to an array. What am I doing wrong?
我是 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()
Error
错误
Traceback (most recent call last):
File "D:\workspace\CATS Parser\cats-post.py", line 31, in e_rcs.append = float(cols.group(2)) AttributeError: 'list' object attribute 'append' is read-only attributes (assign to .append)
回溯(最近一次调用):
文件“D:\workspace\CATS Parser\cats-post.py”,第 31 行,在 e_rcs.append = float(cols.group(2)) AttributeError: 'list' object attribute ' append' 是只读属性(分配给 .append)
回答by S.Lott
Do you want to append to the array?
你想追加到数组吗?
e_rcs.append( float(cols.group(2)) )
Doing this: e_rcs.append = float(cols.group(2))
replaces the append
method of the array e-rcs
with a floating-point value. Rarely something you want to do.
这样做:e_rcs.append = float(cols.group(2))
用浮点值替换append
数组的方法e-rcs
。很少有你想做的事情。
回答by Brent Newey
You are assigning to the append() function, you want instead to call .append(float(cols.group(2))).
您正在分配给 append() 函数,而是想调用 .append(float(cols.group(2)))。
回答by jamessan
append is a method. You're trying to overwrite it instead of calling it.
append 是一种方法。你试图覆盖它而不是调用它。
e_rcs.append(float(cols.group(2)))
回答by Michael Dillon
Try this instead:
试试这个:
import re
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 = [] # make an empty list
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)) ) # add another float to the list
f.close()
In Python you would only use array.array when you need to control the binary layout of your storage, i.e. a plain array of bytes in RAM.
在 Python 中,当您需要控制存储的二进制布局(即 RAM 中的普通字节数组)时,您只会使用 array.array。
If you are going to be doing a lot of scientific data analysis, then you should have a look at the NumPymodule which supports n-dimensional arrays. Think of NumPy as a replacement for FORTRAN in doing mathematics and data analysis.
如果您要进行大量科学数据分析,那么您应该查看支持 n 维数组的NumPy模块。在进行数学和数据分析时,可以将 NumPy 视为 FORTRAN 的替代品。