【发布时间】:2015-11-27 03:36:39
【问题描述】:
人们已经给出的答案是:在另一个变量的赋值中使用一个变量的值:
- 创建字典,并且,
- 使用 dict[oldVariable] 而不是定义一个新变量
我认为这不适用于我正在尝试做的事情......
我正在尝试为向量定义一个类,该类将列表作为输入,并为列表的每个元素在向量中分配一个条目。
我的代码现在看起来像这样:
class vector:
def __init__(self, entries):
for dim in range(len(entries)):
for entry in entries:
self.dim = entry #here I want to assign self.1, self.2, etc all the way to however
#many elements are in entries, but I can't replace self.dim with
# dict[dim]
def __str__(self):
string = []
for entry in range(1,4):
string.append(self.entry)
print(string)
我该怎么做?
【问题讨论】:
-
你甚至不能在 Python 中做
self.1。为什么你坚持这样做而不是字典或简单的列表?我的意思是,self.elements[0]可以正常工作...
标签: python class dictionary