Python 创建实例列表和每个实例变量列表

Python 创建实例列表和每个实例变量列表,python,class,instance,Python,Class,Instance,我试图使用以下代码列出一个类的实例,每个实例具有不同的属性值: lines_stripped = ['Name', 'Age', 'Score', 'John Whorfin', '52', '1.553', 'John Emdall', '35', '1.21', 'John Parker', '41', '1.987', 'John Gomez', '33', '1.305', 'John Yaya', '41', '1.411', 'John Gant', '39', '1.6821']

我试图使用以下代码列出一个类的实例,每个实例具有不同的属性值:

lines_stripped = ['Name', 'Age', 'Score', 'John Whorfin', '52', '1.553', 'John Emdall', '35', '1.21', 'John Parker', '41', '1.987', 'John Gomez', '33', '1.305', 'John Yaya', '41', '1.411', 'John Gant', '39', '1.6821']
header = lines_stripped[0:3]
lines_stripped = lines_stripped[3:] 

class Lectroid():

    def __init__ (self, Name, Age, Score):

        self.name = Name
        self.age = Age
        self.score = Score


lectroidNames = range(0,6)  
#lectroidNames = lectroidNames.append('Lectroid')
i = 0
j = 1
k = 2
x = 0
while x < 6:
    lectroidNames[x] = Lectroid(lines_stripped[0], lines_stripped[1], lines_stripped[2]) #How can I not have constantly overriding lectroids
    i += 3
    j += 3
    k += 3
    x += 1
我想列出每个例子的分数。我尝试使用
print name.score
执行此操作,但收到以下错误消息:
AttributeError:“list”对象没有属性“score”

当我使每个实例都具有属性分数时,为什么会收到此错误消息?
如何获得每个实例的分数列表?

您可以定义一个空列表,然后附加:

lectroidNames =[]

i = 0
j = 1
k = 2
x = 0
while x < 6:
    lectroidNames.append(Lectroid(lines_stripped[i], lines_stripped[j], lines_stripped[k])) 
    i += 3
    j += 3
    k += 3
    x += 1
结果:

John Whorfin
John Emdall

你可以这样做

$ cat make.py
lines_stripped = ['Name', 'Age', 'Score', 'John Whorfin', '52', '1.553', 'John Emdall', '35', '1.21', 'John Parker', '41', '1.987', 'John Gomez', '33', '1.305', 'John Yaya', '41', '1.411', 'John Gant', '39', '1.6821']
header = lines_stripped[0:3]
lines_stripped = lines_stripped[3:] 

class Lectroid():
        def __init__ (self, name, age, score):
                self.name = name
                self.age = age
                self.score = score


instances = []
data = iter(lines_stripped)
for _ in range(int(len(lines_stripped) / len(header))):
    name, age, score = [next(data) for _ in range(len(header)]
    instances.append(Lectroid(name, age, score))

for instance in instances:
    print(instance.name, instance.age, instance.score)
输出:

$ python make.py
('John Whorfin', '52', '1.553')
('John Emdall', '35', '1.21')
('John Parker', '41', '1.987')
('John Gomez', '33', '1.305')
('John Yaya', '41', '1.411')
('John Gant', '39', '1.6821')

你到底想在
ElectroidName
中得到什么?你是想让ElectroidName在打印时显示它的属性(这样你就可以打印一个ElectroidName列表),还是只想让ElectroidName列表中的分数列表?我想让ElectroidName成为一个实例列表-我现在意识到这本可以更好地命名。我想要一个不同的变量来包含分数列表,例如
scores
。我希望
electroidnames
成为一个列表,我可以从中查找其他属性。这取决于您从何处获得的
行以及您想要的确切输出是否有一种方法可以在不使用itertools或make.py的情况下获得类似的结果?另外,我使用的是python 2.7更新版,没有使用
itertools
,这应该可以在
python2
中使用,但它没有回答这个问题。
$ cat make.py
lines_stripped = ['Name', 'Age', 'Score', 'John Whorfin', '52', '1.553', 'John Emdall', '35', '1.21', 'John Parker', '41', '1.987', 'John Gomez', '33', '1.305', 'John Yaya', '41', '1.411', 'John Gant', '39', '1.6821']
header = lines_stripped[0:3]
lines_stripped = lines_stripped[3:] 

class Lectroid():
        def __init__ (self, name, age, score):
                self.name = name
                self.age = age
                self.score = score


instances = []
data = iter(lines_stripped)
for _ in range(int(len(lines_stripped) / len(header))):
    name, age, score = [next(data) for _ in range(len(header)]
    instances.append(Lectroid(name, age, score))

for instance in instances:
    print(instance.name, instance.age, instance.score)
$ python make.py
('John Whorfin', '52', '1.553')
('John Emdall', '35', '1.21')
('John Parker', '41', '1.987')
('John Gomez', '33', '1.305')
('John Yaya', '41', '1.411')
('John Gant', '39', '1.6821')