python 中,字典中的kay与list列表中的值比较,有相同的,则把相同的字典重新添加到新的字典中.

1个回答

  • import os

    a = {'samsungGalaxy+Nexus': '33', 'alpsKPT+I5': '1', 'samsungGT-I9003': '1', 'dellDell+V04B': '1', 'samsungGT-I9000': '5', 'lenovoIdeaTabA2109A': '3', 'huaweiT8620': '1', 'unknownsoaiy': '1'}

    b = ['samsungGalaxy+Nexus', 'alpsKPT+I5', 'samsungGT-I9003', 'teclastP88%28H3D7%29', 'rockchiprk30sdk', 'viewsonicN1010']

    # IF SAME APPEND TO DICT C,ELSE TO LIST D

    sameList = []

    for keys in a.keys():

    if keys in b:

    sameList.append((keys, a[keys]))

    a.pop(keys)

    b.remove(keys)

    dictSame = dict(sameList)

    print "Same:n%s" % dictSame

    print "Different:n%s" % a

    os.system("pause")