【发布时间】:2017-06-21 07:51:09
【问题描述】:
我只是从#C 开始接触 python,我有一个我无法找到答案的问题,也许我无法正确地形成一个问题
使用时我需要这个来创建两个列表:load(positives) 和 load(negatives),positives 是文件的路径。从#C开始,我习惯于使用这种结构,而不是仅仅使用另一个变量再次复制相同的代码,例如。如果我需要 5 个列表怎么办?使用此代码,我只能访问 self.dictionary 变量,但绝不可以访问 self.positives 和 self.negatives
我收到错误 AttributeError: 'Analyzer' object has no attribute 'positives' at line 'for p in self.positives:'
主要问题是:如何让 self.dictionary = [] 从参数名称创建列表变量 - self.positives 和 self.negatives 我稍后在代码中需要
def load(self, dictionary):
i = 0
self.dictionary = []
with open(dictionary) as lines:
for line in lines:
#some more code
self.dictionary.append(0)
self.dictionary[i] = line
i+=1
#later in code
for p in self.positives:
if text == p:
score += 1
for p in self.negatives:
if text == p:
score -= 1
#structure of a program:
class Analyzer():
def load()
def init()
load(positives)
load(negatives)
def analyze()
for p in self.positives
【问题讨论】:
-
您是否将
load定义为类外的方法或函数?我怀疑是后者。 -
我不明白你的问题。你的代码到底是怎么不工作的?
-
什么是字典?它持有什么样的价值观?
-
字典参数得到肯定和否定 - 它保存文件的路径,所以我猜是一个字符串
标签: python list variables arguments names