【问题标题】:Matching keys in different lists of dicts匹配不同字典列表中的键
【发布时间】:2017-02-11 20:23:45
【问题描述】:

我有一个 csv.DictReader 字典实例和一个字典列表。

实例:

{'Salary': '3000', 'Name': 'James Jones', 'GameInfo': 'Den@Cle 07:30PM ET', 
 'AvgPointsPerGame': '4.883', 'teamAbbrev': 'Cle', 'Position': 'SG'}
{'Salary': '3000', 'Name': 'Justin Anderson', 'GameInfo': 'Orl@Dal 09:00PM ET',
 'AvgPointsPerGame': '13.161', 'teamAbbrev': 'Dal', 'Position': 'SF'}

列表:

[

    {'playername': 'Justin Anderson', 'points': '6.94'},

    {'playername': 'DeAndre Liggins', 'points': '11.4'},

]

我不知道如何遍历这些字典列表,匹配 Name 和 playername 键,然后从一个字典中吐出 ['Name'] 和从匹配的字典中吐出 ['points']。在上面的示例中,我将匹配两组 dicts 中的 Justin Anderson,然后打印出 Justin Anderson, 6.94

应用程序的核心采用 2 个 CSV 并将它们制作成字典列表。

【问题讨论】:

    标签: python python-2.7 dictionary


    【解决方案1】:

    这种方式效率不高,但不需要任何预处理:

    # Instead of your CSVReader:
    dicts = [{'Salary': '3000', 'Name': 'James Jones', 'GameInfo': 'Den@Cle 07:30PM ET', 'AvgPointsPerGame': '4.883', 'teamAbbrev': 'Cle', 'Position': 'SG'},
             {'Salary': '3000', 'Name': 'Justin Anderson', 'GameInfo': 'Orl@Dal 09:00PM ET', 'AvgPointsPerGame': '13.161', 'teamAbbrev': 'Dal', 'Position': 'SF'}]
    
    list_of_dicts = [
        {'playername': 'Justin Anderson', 'points': '6.94'},
        {'playername': 'DeAndre Liggins', 'points': '11.4'},
    ]
    
    # For each dictionary in the CSVReader
    for dct in dicts:
        # For each dictionary in your list of dictionaries
        for subdict in list_of_dicts:
            # Check if the name and playername matches
            if dct['Name'] == subdict['playername']:
                # I just print out the results, you need to do your logic here
                print(dct['Name'])
                print(dct)
                print('matching')
                print(subdict)
    

    然后打印出来:

    Justin Anderson
    {'Salary': '3000', 'Name': 'Justin Anderson', 'GameInfo': 'Orl@Dal 09:00PM ET', 'AvgPointsPerGame': '13.161', 'Position': 'SF', 'teamAbbrev': 'Dal'}
    matching
    {'playername': 'Justin Anderson', 'points': '6.94'}
    

    如果你希望它比你应该预处理你的字典列表更快,这样你就可以简单地查找playername

    >>> dict_of_dicts = {dct['playername']: dct for dct in list_of_dicts}
    >>> dict_of_dicts
    {'DeAndre Liggins': {'playername': 'DeAndre Liggins', 'points': '11.4'},
     'Justin Anderson': {'playername': 'Justin Anderson', 'points': '6.94'}}
    

    然后循环简化为:

    for dct in dicts:
        if dct['Name'] in dict_of_dicts:
            print(dct['Name'])
            print(dct)
            print('matching')
            print(dict_of_dicts[dct['Name']])
    

    给出相同的结果。

    【讨论】:

    • 感谢您的回答。我如何修改它以显示名称、平均每游戏点数、点数?我似乎无法理解“dict_of_dicts = {dct ['playername']:dct for dct in list_of_dicts}”的工作原理,我需要研究语法。但它正在按照您的描述工作。
    • 你可以做print(dct['Name'], dct['AvgPointsPerGame'], dict_of_dicts[dct['Name']]['Points']){x:y for ... in ....} 是字典理解:stackoverflow.com/documentation/python/196/comprehensions/738/…
    • 这很好地引导我找到了解决方案。谢谢 MSeifert。
    猜你喜欢
    • 2014-10-03
    • 2022-10-14
    • 2020-09-10
    • 2021-01-10
    • 1970-01-01
    • 2021-11-17
    • 2020-11-04
    • 1970-01-01
    • 2020-03-31
    相关资源
    最近更新 更多