当我打印出前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()
向类中添加^{} 和^{} 方法:
演示:
当对象转换为字符串时,
__str__
方法会自动调用,就像print()
函数一样。你知道吗__repr__
方法用于repr()
表示以及在列表和其他容器中显示对象时。你知道吗相关问题 更多 >
编程相关推荐