【问题标题】:Add Objects to Lists in Python在 Python 中将对象添加到列表中
【发布时间】:2020-03-28 11:22:44
【问题描述】:

我刚开始学习 python,这是我的问题,我对 python 很陌生 - 我有两个 python 文件都在同一个文件夹中..

1.问题.py

class Question:

    def __init__(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer

    def chkQuestion(self):
        if self.answer != "":
            return True

2。 quiz.py

questions_prompts = [
    "what color are apples? \n (a) red \n(b) green \n(c) orange",
    "what color are bananas? \n (a) Teal \n(b) Magenta \n(c) Yellow",
    "what color are strawberries? \n (a) yellow \n(b) red \n(c) blue"
]

questions = []

answers = "bcd"

for i in range(3):
    pos = int(i)-1
    print(i)
    questions.append(Question(questions_prompts[pos], answers[pos]))

print(questions[0].prompt)

当我打印问题列表时 - 它会按预期显示添加到列表中的所有问题,但是当我尝试打印出第一个问题(问题 [0])的提示属性时,它会给出此错误

AttributeError: 'str' object has no attribute 'prompt'

请问我做错了什么,如何将对象推送到列表并按预期读取属性。本教程创建了新的问题对象并手动传递了值,但我很好奇如果我有 300 多个问题,我无法手动执行此操作,所以我创建了循环并且我被卡住了。

【问题讨论】:

  • answers[pos] 是什么?
  • 1.为什么列表questions 没有初始化? 2. 为什么你用pos 索引questions_promptsanswers,而不是i,它的值是0、1、2? 3.answers是什么?
  • @maurice meyer,抱歉我刚刚调整了代码。
  • 我建议在 Discord 服务器上向初学者提问。如果您以非专业人士的身份在这里提问,您将得到的只是刻薄的答案和反对票。我强烈建议去一个更友好的社区。我可以推荐 The Coding Den Discord 服务器,他们有所有语言的单独频道,而且比这里的人友好得多。
  • @Aci 非常感谢,我会听取您的建议,在做了多年的 php 之后尽我所能迁移到 python .. 但是 stackoverflow 不友好..

标签: python python-3.x list class object


【解决方案1】:

虽然不清楚你要做什么,所以我从编写的代码中假设:

class Question:
    def __init__(self, prompt):
        self.prompt = prompt

    def execute(self):
        self.answer = input(self.prompt)

    def chkQuestion(self):
        if self.answer != "":
            return True


questions_prompts = [
    "what color are apples? \n (a) red \n(b) green \n(c) orange",
    "what color are bananas? \n (a) Teal \n(b) Magenta \n(c) Yellow",
    "what color are strawberries? \n (a) yellow \n(b) red \n(c) blue"
]

questions = []

for question in questions_prompts:
    q = Question(question)
    q.execute()
    questions.append(q)

print(questions)

for i, q in enumerate(questions):
    print("Answer for question %s was: %s" % (i, q.answer))

输出:

[<__main__.Question object at 0x10e2d67d0>, <__main__.Question object at 0x10e2d6810>, <__main__.Question object at 0x10e2d6850>]
Answer for question 0 was: a
Answer for question 1 was: b
Answer for question 2 was: c

【讨论】:

    【解决方案2】:

    您需要适应的工作示例:

    import numpy as np
    
    class Foo:
        def __init__(self, a, b):
            self.a = a
            self.b = b
    
    list_of_foos = list()
    
    a_rand = np.random.rand(100) * 1000 # Random a
    b_rand = np.random.rand(100) * 1000 # Random b
    
    for i in range(100): # Generate 100 Objects
        Obj = Foo(a_rand[i], b_rand[i])
        list_of_foos.append(Obj)
    
    print (list_of_foos[20].a)
    print (a_rand[20])
    

    在这种情况下,我创建了一个对象列表,最后,我从第 20 个对象打印属性 a,它与列表 a_rand 的第 20 个元素相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 2018-07-23
      • 1970-01-01
      • 2016-01-09
      相关资源
      最近更新 更多