将列表从一个类方法转移到另一个类方法(Transferring list from one class method to another)

编程入门 行业动态 更新时间:2024-10-25 14:25:34
将列表从一个类方法转移到另一个类方法(Transferring list from one class method to another)

因此,在我所拥有的课程中,我会逐行地从不同列表中收集的信息创建一个列表,并且到目前为止已经顺利进行。 我的问题是,使用这个新制作的列表,我需要在同一个类中使用不同的方法,以便以结构化的方式打印列表的信息。 我不确定如何将新列表从一种方法转移到另一种方法,以便我可以开始打印过程。 在我的主程序中,利用字典(字符串值作为键,类对象作为值)来引用类方法,我有输入

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,但是一旦我能够利用方法中的列表,那部分就会很容易。

So in a class that I have, I create a list from information gathered in a different list line by line, and so far that has gone smoothly. My issue is that with this newly made list, I need to utilize a different method in the same class in order to print the list's information in a structured manner. I am unsure of exactly how to get the new list from one method to another so that I can begin the printing process. In my main program, that utilizes a dictionary (string value as key, class object as value) to refer to the class methods, I have the input of

playerDict[nameChoice].calc(basicList)

which takes me to the method for the given player, and then compiles the list. The two methods are as follows

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.'''

So the list I need in the printInfo method is ratingList. In my main program, I have invoked the two separately, using playerDict[nameChoice].calc(basicList) and playerDict[nameChoice].printInfo(), but I need to find a way to get the ratingList into the printInfo method in order to print out the information in the list in the specified manner. How would I go about doing this? I dont have printInfo completed yet but that part will come easily once I am capable of utilizing the list in the method.

最满意答案

def printInfo(self, basicList): ratingList = self.calc(basicList) ...

请注意, printInfo需要列表。

def printInfo(self, basicList): ratingList = self.calc(basicList) ...

Note that printInfo needs the list as well.

更多推荐

本文发布于:2023-08-05 00:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424485.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   转移到   列表   Transferring   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!