大家好。我有一个字典列表,里面有不同的游戏。游戏是按顺序进行的。每个游戏都有不同数量的按键。字典中有几个相同的键,但含义不同。问题是我还没有想出如何使用特定的键将游戏划分到不同的字典中。
list = [{'SA': '1'}, {'ZA': 'AFRICA: African Nations Championship'}, {'ZEE': 'fZS33RAH'}, {'ZB': '1'}, {'ZY': 'Africa'}, {'ZC': 'AuZlotWi'}, {'ZD': 'p'}, {'ZE': 'hQF2OO7Q'}, {'ZF': '0'}, {'ZO': '0'}, {'ZG': '1'}, {'ZH': '1_fZS33RAH'}, {'ZJ': '2'}, {'ZL': '/football/africa/african-nations-championship/'}, {'ZX': '00Africa 006Africa0040000000001000African Nati028onship000'}, {'ZXF': '00Africa 006Africa0040000000001000African Nati028onship000'}, {'ZCC': '0'}, {'ZAF': 'Africa'}, {'AA': 'lv6E76jD'}, {'AD': '1611514800'}, {'ADE': '1611514800'}, {'AB': '3'}, {'CR': '3'}, {'AC': '3'}, {'CX': 'Burkina Faso'}, {'RW': '0'}, {'AX': '1'}, {'AO': '1611521595'}, {'BX': '-1'}, {'WQ': ''}, {'WM': 'BFA'}, {'AE': 'Burkina Faso'}, {'JA': 'vXsxyJv4'}, {'WU': 'burkina-faso'}, {'AS': '0'}, {'AZ': '0'}, {'GRA': '0'}, {'AG': '0'}, {'BA': '0'}, {'BC': '0'}, {'OA': '6FkKkDT0-d4SDTRYi.png'}, {'WN': 'CAM'}, {'AF': 'Cameroon'}, {'JB': 'INttzwfA'}, {'WV': 'cameroon'}, {'AS': '0'}, {'AZ': '0'}, {'GRB': '0'}, {'AH': '0'}, {'BB': '0'}, {'BD': '0'}, {'OB': '4MsJXS86-CMP7lutI.png'}, {'AA': 'jqpcWkTD'}, {'AD': '1611514800'}, {'ADE': '1611514800'}, {'AB': '3'}, {'CR': '3'}, {'AC': '3'}, {'CX': 'Zimbabwe'}, {'RW': '0'}, {'AX': '1'}, {'AO': '1611521438'}, {'BX': '-1'}, {'WQ': ''}, {'WM': 'ZIM'}, {'AE': 'Zimbabwe'}, {'JA': 'CptpZd9G'}, {'WU': 'zimbabwe'}, {'GRA': '0'}, {'AG': '0'}, {'BA': '0'}, {'BC': '0'}, {'OA': 'bBXMPUhT-QwFad6pm.png'}, {'WN': 'MAL'}, {'AF': 'Mali'}, {'JB': 'AVbmYGOM'}, {'WV': 'mali'}, {'AS': '2'}, {'AZ': '2'}, {'GRB': '0'}, {'AH': '1'}, {'BB': '1'}, {'BD': '0'}, {'OB': 'txCAqWjC-dWt4REwT.png'}, {'ZA': 'ALBANIA: Superliga'}, {'ZEE': 'nwPDBpVc'}, {'ZB': '17'}, {'ZY': 'Albania'}, {'ZC': 'ULkLo4dK'}, {'ZD': 'p'}, {'ZE': 'OKzcNHo0'}, {'ZF': '0'}, {'ZO': '0'}, {'ZG': '1'}, {'ZH': '17_nwPDBpVc'}, {'ZJ': '2'}, {'ZL': '/football/albania/superliga/'}, {'ZX': '00Albania 007lbania0000000000001000Superliga 009erliga000'}, {'ZXF': '00Albania 007lbania0000000000001000Superliga 009erliga000'}, {'ZCC': '0'}, {'ZAF': 'Albania'}]
游戏键是:
game_keys = ['AA', 'BA', 'BB', 'BC', 'BD', 'AB', 'AC', 'AG', 'AH', 'AT', 'AU', 'BI', 'BJ']
我试图创建一个循环来检查每个元素是否存在一个键,但结果是完全没有意义的:
match = []
for item in list:
game_keys = ['AA', 'BA', 'BB', 'BC', 'BD', 'AB', 'AC', 'AG', 'AH', 'AT', 'AU', 'BI', 'BJ']
if item.keys() == game_keys[0]:
match_id = list(item.values())[0]
elif item.keys() == game_keys[1]:
home_HT = list(item.values())[0]
是否可以使用"game_keys"将这个字典列表划分为不同的字典(游戏)?预期的结果:
games = [
{
'match_id': 'lv6E76jD' # game_keys 'AA',
'home_HT': '0' # game_keys 'BA',
'away_HT': '0' # game_keys 'BB',
'home_ST': '0' # game_keys 'BC'
},
# ...
# etc.
# ...
{
'match_id': 'jqpcWkTD',
'home_HT': '0',
'away_HT': 0,
'home_ST': 0
}
]
如果目标是从由键值对组成的大量输入流中收集有价值的信息,则在重复的"训练"中排序;已知的键也是一样的,下面是我解决这个问题的方法。我将重点关注作为预期结果呈现的四个关键。我还使用最后一个键'BC'
作为触发器来完成提取的字典。这种特殊情况也是硬编码game_keys的原因。
games = [] # for collecting all results
match = {} # for collecting elements of single match (reused)
for item in input_list:
key, value = list(item.items())[0]
if 'AA' == key:
match['match_id'] = value
elif 'BA' == key:
match['home_HT'] = value
elif 'BB' == key:
match['away_HT'] = value
elif 'BC' == key:
match['home_ST'] = value
games.append(match)
match = {} # reset for next match
使用关键字"in"检查值是否存在于哈希集或字典中。在您的初始场景中,将列表game_keys转换为哈希集更有意义,但很可能需要将其转换为字典以映射所需的结果。
myListOfDictionaries = [{'SA': '1'}, {'AC': '3'}, {'BA': '0'}, {'AA': 'jqpcWkTD'}]
game_keys = set(['AA', 'BA', 'BB'])
match = {}
for myDictionary in myListOfDictionaries:
for key in myDictionary:
if(key in game_keys):
if(key in match):
match[key].append(myDictionary[key])
else:
match[key] = [myDictionary[key]]
为了完成你的任务,我假设你需要game_keys是一个字典,它的值是"match_id"。为关键字"AA", "为关键"BA"…
这样可以更灵活地创建所需的结果。
这不是你问题的最终解决方案,我不相信如果我简单地给你解决方案你会学到什么。我相信有了这段代码,你可能能够解决创建字典的字典的问题。
在我的示例中,我有一个字典"match",我查找键是否存在,如果不存在,我向其中添加一个单项列表…如果它存在,我就知道有一个至少包含一项的列表,所以我就给它添加更多的项。
您可以使用相同的逻辑创建dictionary的dictionary。