代码之家  ›  专栏  ›  技术社区  ›  x__SHARINGAN____x

如何比较两个python字典并找到缺失的元素?

  •  -4
  • x__SHARINGAN____x  · 技术社区  · 1 年前

    我遇到了一个问题,我已经尝试了好几天,但没有任何结果,我想比较两本词典,一本词典有“赛前”足球比赛,另一本词典则有“现场”足球比赛。

    我想把它们相互比较一下,如果没有赛前比赛直播,你必须打印出来,现在我会更好地显示代码

    示例1

    `import json
    
    pre = [{
            "Sport": "Calcio",
            "Championship": "Italia - Serie A",
            "Home": "Genoa",
            "Away": "Inter",
            "Match Full": "Genoa v Inter",
            "URL": "https://www.bet365.it/#/AC/B1/C1/D8/E158355896/F3/I0/",
            "Start Data": "19 Lug",
            "Start Time": "21:30"
        },
        {
            "Sport": "Calcio",
            "Championship": "Italia - Serie A",
            "Home": "Parma",
            "Away": "Fiorentina",
            "Match Full": "Parma v Fiorentina",
            "URL": "https://www.bet365.it/#/AC/B1/C1/D8/E158355904/F3/I0/",
            "Start Data": "17 Ago",
            "Start Time": "18:30"
        }]
    
    live = [{
            "Sport": "Calcio",
            "Championship": "Myanmar - Lega Nazionale",
            "Home": "Dagon Star United FC",
            "Away": "Ispe FC",
            "Match Full": "Dagon Star United FC v Ispe FC"
        },
        {
            "Sport": "Calcio",
            "Championship": "Italia - Serie A",
            "Home": "Genoa",
            "Away": "Inter",
            "Match Full": "Genoa v Inter"
        }]
    
    check = [[x for x in pre if x['Match Full'] != i['Match Full']] for i in live]
    
    print(check)`
    

    我没有收到预期的结果,我还尝试了以下代码,但没有收到正确的结果

    示例2

    `import json
    
    pre = [{
            "Sport": "Calcio",
            "Championship": "Italia - Serie A",
            "Home": "Genoa",
            "Away": "Inter",
            "Match Full": "Genoa v Inter",
            "URL": "https://www.bet365.it/#/AC/B1/C1/D8/E158355896/F3/I0/",
            "Start Data": "19 Lug",
            "Start Time": "21:30"
        },
        {
            "Sport": "Calcio",
            "Championship": "Italia - Serie A",
            "Home": "Parma",
            "Away": "Fiorentina",
            "Match Full": "Parma v Fiorentina",
            "URL": "https://www.bet365.it/#/AC/B1/C1/D8/E158355904/F3/I0/",
            "Start Data": "17 Ago",
            "Start Time": "18:30"
        }]
    
    live = [{
            "Sport": "Calcio",
            "Championship": "Myanmar - Lega Nazionale",
            "Home": "Dagon Star United FC",
            "Away": "Ispe FC",
            "Match Full": "Dagon Star United FC v Ispe FC"
        },
        {
            "Sport": "Calcio",
            "Championship": "Italia - Serie A",
            "Home": "Genoa",
            "Away": "Inter",
            "Match Full": "Genoa v Inter"
        }]
    
    for x in pre:
        for i in live:
            if x['Match Full'] != i['Match Full']:
                print(x['Match Full'])`
    

    我想得到的只是“直播”词典中缺少的赛前信息,在这种情况下,它应该只打印“帕尔马对佛罗伦萨”,因为字典中没有

    任何解决方案都将不胜感激,提前感谢。

    打印(x[“匹配完整”])

    缺少比赛

    2 回复  |  直到 1 年前
        1
  •  1
  •   Suramuthu R    1 年前
    #Create a list of value of 'Match Full' from live
    live_lst = [x["Match Full"] for x in live] 
    
    for x in pre:
        if x["Match Full"] not in live_lst:
            print(x["Match Full"])
    
    #Output : Parma v Fiorentina
    
        2
  •  1
  •   folen gateis    1 年前

    这应该行了

    def get_set(matches):
        return set([match_['Match Full'] for match_ in matches])
        
    pre_set= get_set(pre)
    live_set = get_set(live)
    print(pre_set-live_set) # {'Parma v Fiorentina'}