编程语言
首页 > 编程语言> > Python 二级试题 有关选举问题选班长选村长

Python 二级试题 有关选举问题选班长选村长

作者:互联网

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])的运用

 

标签:试题,encoding,Python,ls,vote,fi,班长,fo,name
来源: https://www.cnblogs.com/PythonBo/p/15845323.html