我对python相当陌生。我试图定义一个类,然后我想从一个文件创建一个实例,然后引用它的特定部分,但似乎不能。这是 Python 3.3.0
这里是课堂......
class Teams():
def __init__(self, ID = None, Team = None, R = None, W = None, L = None):
self._items = [ [] for i in range(5) ]
self.Count = 0
def addTeam(self, ID, Team, R=None, W = 0, L = 0):
self._items[0].append(ID)
self._items[1].append(Team)
self._items[2].append(R)
self._items[3].append(W)
self._items[4].append(L)
self.Count += 1
def addTeamsFromFile(self, filename):
inputFile = open(filename, 'r')
for line in inputFile:
words = line.split(',')
self.addTeam(words[0], words[1], words[2], words[3], words[4])
def __len__(self):
return self.Count
这是 Main 中的代码
startFileName = 'file_test.txt'
filename = startFileName
###########
myTestData = Teams()
myTestData.addTeamsFromFile(startFileName)
文件中的样本数据
100,AAAA,106,5,0
200,BBBB,88,3,2
300,CCCC,45,1,4
400,DDDD,67,3,2
500,EEEE,90,4,1
我想我在这里很好(不是 100% 肯定),但现在我如何参考这些数据来查看......我没有正确创建类吗?如何查看一个实例是否大于另一个实例...
即,myTestData[2][2] > myTestData[3][2]
<-----这是我感到困惑的地方,因为这不起作用