比较两个列表并将较大的值弹出到新列表(Python)

时间:2013-10-12 17:11:42

标签: python

我必须比较两个列表并将最高值添加到新列表中。我想将.pop函数与我现在的代码一起使用。这是家庭作业,但我的所有资源都已关闭,所以任何帮助将不胜感激。感谢。

如果有帮助,这是我的代码。

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:
        line1 = line.split( "," )
        names = mkList( line1[0], line1[1], line1[2] )
        lst.append( names )
        if names.gender == 'F':
            femaleLst += [ line1 ]
        else:
            maleLst += [ line1 ]
    while len( lst ) < 20:
        if name.occurences( maleLst ) > name.occurences( femaleLst ):
            maleLst.pop(0) += [ lst ]
        else:
            femaleLst.pop(0) += [ lst ]
    print( femaleLst )


main()

0 个答案:

没有答案