【问题标题】:Transferring list from one class method to another将列表从一个类方法转移到另一个
【发布时间】:2016-08-25 20:47:16
【问题描述】:

因此,在我的课程中,我根据收集在不同列表中的信息逐行创建了一个列表,到目前为止一切顺利。我的问题是,对于这个新制作的列表,我需要在同一个类中使用不同的方法,以便以结构化的方式打印列表的信息。我不确定如何将新列表从一种方法获取到另一种方法,以便开始打印过程。在我的主程序中,使用字典(字符串值作为键,类对象作为值)来引用类方法,我有

的输入

playerDict[nameChoice].calc(basicList)

它将我带到给定播放器的方法,然后编译列表。两种方法如下

    def calc(self, sublist):
    '''calculate a passer rating for one sub-list year in the info list'''

    ratingList = []
    count = 0
    for line in self.info:
        C = ((((int(line[3]) / int(line[4]))*100) - 30)/20)
        if C > 2.375:
            C = 2.375
        Y = (((int(line[5]) / int(line[4])) - 3) * .25)
        if Y > 2.375:
            Y = 2.375
        T = (((int(line[6])) / int(line[4])) * 20)
        if T > 2.375:
            T = 2.375
        I = 2.375 - ((int(line[7])/int(line[4])) * 25)
        ratingtop = (C + Y + T + I)
        ratingbot = 6
        yearRating = float((ratingtop / ratingbot) * 100)
        ratingList.append([self.info[count][0], self.info[count][2], yearRating])
        print(ratingList)
        count += 1
    ratingList.sort()
    print(ratingList)
    print_ratings = self.printInfo
    return ratingList

def printInfo(self):
    '''print individual year information about this player including each years passer rating. The list should be in year order. Use calc to assist.'''

所以我在 printInfo 方法中需要的列表是 ratingList。在我的主程序中,我已经分别调用了这两个,使用playerDict[nameChoice].calc(basicList)playerDict[nameChoice].printInfo(),但是我需要找到一种方法将ratingList放入printInfo方法中,以便以指定的方式打印出列表中的信息.我该怎么做呢?我还没有完成 printInfo,但是一旦我能够使用方法中的列表,那部分就很容易了。

【问题讨论】:

    标签: python class methods return


    【解决方案1】:
    def printInfo(self, basicList):
        ratingList = self.calc(basicList)
        ...
    

    请注意,printInfo 也需要该列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多