当我打印出前 20 次出现的列表时,我得到的只是对象位置,而不是我需要的实际数据。此外,如果您有任何改进的想法,将不胜感激。请帮忙。谢谢。
这是我的代码。
class topList():
__slots__ = ( "name", "gender", "occurences" )
def mkList( name, gender, occurences ):
find = topList()
find.name = name
find.gender = gender
find.occurences = occurences
return find
def main():
year = input( 'Enter year: ' )
file = open( 'yob' + year + '.txt' )
lst = []
femaleLst = []
maleLst = []
for line in file:
line = line.strip().split( "," )
names = mkList( line[0], line[1], line[2] )
lst.append( names )
if names.gender == 'F':
femaleLst += [ line ]
else:
maleLst += [ line ]
while len( lst ) < 20:
male = maleLst.pop()
female = femaleLst.pop()
if maleLst.occurences > femaleLst.occurences:
lst += [ male ]
else:
lst += [ female ]
print( lst[ : 20] )
main()