fi = open("name.txt","r",encoding = "utf-8") fo = open("vote.txt","r",encoding = "utf-8") names = fi.readlines() votes = fo.readlines() d={} n = 0 for vote in votes: ls = vote.split() if len(ls)==1: for name in names: if name.count(ls[0])==1: n+=1 d[ls[0]] = d.get(ls[0],0)+1 ls = list(d.items()) ls.sort(key = lambda x:x[1],reverse = True) name,score = ls[0] print("班长为:{},有效票数为:{}".format(name,score)) fi.close() fo.close()
##name 为选举人名单,vote为投票选举出的名单。 为了保证不重名,有效票,并且统计总数。注意name.count(ls[0])的运用