比较两个列表并将较大的值弹出到新列表(Python)
我必须比较两个列表并将最大值添加到新列表中。我想在我现在的代码中使用.pop函数。这是家庭作业,但我所有的资源都被关闭了,所以任何帮助都将不胜感激。谢谢 这是我的代码,如果有帮助的话比较两个列表并将较大的值弹出到新列表(Python),python,Python,我必须比较两个列表并将最大值添加到新列表中。我想在我现在的代码中使用.pop函数。这是家庭作业,但我所有的资源都被关闭了,所以任何帮助都将不胜感激。谢谢 这是我的代码,如果有帮助的话 class topList(): __slots__ = ( "name", "gender", "occurences" ) def mkList( name, gender, occurences ): find = topList() find.name = name fin
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()
class topList():
__时段=(“姓名”、“性别”、“事件”)
def mkList(姓名、性别、事件):
find=topList()
find.name=name
find.gender=性别
find.occurrences=事件
返回查找
def main():
年份=输入('输入年份:')
文件=打开('yob'+year+'.txt')
lst=[]
femaleLst=[]
maleLst=[]
对于文件中的行:
line1=行分割(“,”)
name=mkList(第1行[0],第1行[1],第1行[2])
lst.append(名称)
如果names.gender='F':
femaleLst+=[line1]
其他:
maleLst+=[line1]
而len(lst)<20:
如果name.occurrences(maleLst)>name.occurrences(femaleLst):
maleLst.pop(0)+=[lst]
其他:
femaleLst.pop(0)+=[lst]
打印(femaleLst)
main()
为什么不使用namedtuple而不是该类?我的作业要求我创建自己的类为了使用类而使用类是愚蠢的。:)在代码中添加一些注释来描述它正在做什么,特别是main()底部的while循环。您是否收到错误/异常?