python - Python 中的对象数组

标签 python arrays list

我想在 python 中定义相同类型的对象数组(我定义的类)。

我尝试使用数组模块:

from array import *
arrayIdentifierName = array("ClassName",[ClassName1,ClassName2,ClassName3])

它说:

TypeError: array() argument 1 must be char, not str

然后我尝试使用列表: 使用列表不会有帮助,因为我在 http://docs.python.org/2/tutorial/datastructures.html 中没有看到方法可以到达每个对象而不将其从列表中删除(例如:list.pop([i])或list.remove(x)将删除该对象,我需要更改它的数据成员之一并保存它)。

大家有什么建议吗?

谢谢

最佳答案

你应该使用列表......
您可以使用列表访问任何对象,并使用其函数执行您想做的任何操作。

class test(object):
    def __init__(self,name):
        self.name=name

    def show(self):
        print self.name

ob1=test('object 1')
ob2=test('object 2')

l=[]
l.append(ob1)
l.append(ob2)

l[0].show()
l[1].show()

关于python - Python 中的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20383364/

相关文章:

python - 如何在 Postgres 中使用 Plpython3 返回结果集

javascript - 打印从数组的索引值中获取的值的数量

Python。列表列表

c - linux/list.h - 如何安全地从列表中删除项目?

python - 返回包含特定字符串的列表列表中的列表索引

python - 如何在Odoo8中调用其他 Sprite 的 Sprite ?

python - 在Python Tkinter : What option can I use to resize a window?

python - 在 Python 中遍历文件的单词

arrays - Matlab all() 函数行号

javascript - 如何从阵列中挑选随机声音? (JS)