字典法求解。字典的遍历:

  • for key,values in dic.items()
while True:
    try:
        n = int(input())
        s = input().split(" ")
        candidate={}
        for i in s:
            candidate[i] = 0
        candidate['Invalid']=0
        m = int(input())
        votes = input().split(" ")
        for v in votes:
            if v not in candidate:
                candidate['Invalid']+=1
            else:
                candidate[v]+=1
        for key,value in candidate.items():
            print(key+" : "+str(value))
    except:
        break