python将使用try except in函数创建的计算值保存到列表
嗨,我有这些嵌套的字典和列表作为我的数据。这是两个匹配项的摘要python将使用try except in函数创建的计算值保存到列表,python,function,Python,Function,嗨,我有这些嵌套的字典和列表作为我的数据。这是两个匹配项的摘要 my_stats = [ {'_id': 'GLEvHIL2020031419A', 'stats': {'data': [ {'area': 'B1', 'key': 'Lineouts', 'possession': 'against', 'second': 7, 'endSecond': 27, 'time': '2020-03-14T12:00:43', 'Lineout Pr
my_stats = [
{'_id': 'GLEvHIL2020031419A', 'stats':
{'data': [
{'area': 'B1', 'key': 'Lineouts', 'possession': 'against', 'second': 7, 'endSecond': 27,
'time': '2020-03-14T12:00:43', 'Lineout Presentation': 'Scrappy', 'Lineout Errors': 'Lost'},
{'area': 'D2', 'key': 'Lineouts', 'possession': 'for', 'second': 344, 'endSecond': 383, 'time':
'2020-03-14T12:53:43', 'Lineout Presentation': 'Goodball', 'Lineout Errors': 'Lost'},
{'area': 'A1', 'key': 'Lineouts', 'possession': 'for', 'second': 354, 'endSecond': 358, 'time':
'2020-03-14T12:56:43', 'Lineout Errors': 'Lost'}]}},
{'_id': 'HJSvMON2020031419A', 'stats':
{'data':
[{'area': 'C1', 'key': 'Kick Off', 'possession': 'against', 'second': 0, 'endSecond': 6, 'time':
'2020-03-14T12:00:06', 'Kick Off Fielded': 'Unsuccessful'},
{'key': 'Passive Tackle', 'possession': 'against', 'second': 9, 'time': '2020-03-14T12:00:09',
'value': 9, 'subMetric': 3, 'rtp': []},
{'area': 'D1', 'key': 'Rucks', 'possession': 'against', 'second': 9, 'endSecond': 19, 'time': '2020-
03-14T12:00:19'},
{'area': 'D1', 'key': 'Lineouts', 'possession': 'for', 'second': 33, 'endSecond': 45, 'time': '2020-
03-14T12:00:45', 'Lineout Formations': '5 Man', 'Lineout Area Of Throw': 'Front', 'Lineout
Contested': 'Contested', 'Lineout Presentation': 'Scrappy', 'Lineout Errors': 'Lost', 'Maul Meters':
'5'},
{'key': 'Neutral Tackle', 'possession': 'for', 'second': 45, 'time': '2020-03-14T12:00:45', 'value':
1, 'subMetric': 2, 'rtp': [{'key': 'Defender in Position', 'possession': 'for', 'second': 49,
'time': '2020-03-14T12:00:49', 'value': 1, 'subMetric': None}]},
{'area': 'D1', 'key': 'Rucks', 'possession': 'for', 'second': 47, 'endSecond': 50, 'time': '2020-03-
14T12:00:50'},
{'key': 'Defender in Position', 'possession': 'for', 'second': 49, 'time': '2020-03-14T12:00:49',
'value': 1, 'subMetric': None, 'rtp': []},
{'key': 'Passive Tackle', 'possession': 'for', 'second': 51, 'time': '2020-03-14T12:00:51', 'value':
7, 'subMetric': 3, 'rtp': [{'key': 'Defender in Position', 'possession': 'for', 'second': 53,
'time': '2020-03-14T12:00:53', 'value': 7, 'subMetric': None}]},
{'key': 'Tackle Assist', 'possession': 'for', 'second': 52, 'time': '2020-03-14T12:00:52', 'value':
8, 'subMetric': None, 'rtp': [{'key': 'Defender in Position', 'possession': 'for', 'second': 53,
'time': '2020-03-14T12:00:53', 'value': 8, 'subMetric': None}]},
{'key': 'Defender in Position', 'possession': 'for', 'second': 53, 'time': '2020-03-14T12:00:53',
'value': 8, 'subMetric': None, 'rtp': []}]}}]
我需要编写一个函数,在其中计算每场比赛赢得的阵容,并保存比赛名称(_id)以及每场比赛的Goodball、scrasty和Drive阵容数量
Lineouts = []
Games = []
def Our_Lineouts_Won():
global Lineouts
Lineouts = (0)
for l in my_stats:
t=(l['stats']['data'])
print(l['_id'])
Games.append(str(l['_id']))
for i in t:
try:
if ((i['key']) == 'Lineouts' and (i['possession'])== 'for'and ((i[('Lineout
Presentation')])== 'Off the Top' or 'Scrappy' or 'Drive' )):
Lineouts += 1
except KeyError:
pass
Our_Lineouts_Won()
我想要的结果是一个字典,其中键是_id,值是计算出的我们的_Lineouts_won()
我已经试着将列表附加到列表中
请帮忙。以下是我如何回答问题的。我创建了一本字典。。。是每场比赛排位赛的缩写。然后我将所有的_id附加到一个游戏列表中 在那之后,我使用try和except,以防止在未捕获任何列输出演示时代码中断 每场比赛都会添加符合标准的阵容,并保存在变量L1下 然后我更新了我的空字典 lpg.update({games:L1}) 如您所见,我使用df=pd.dataframe(list(lpg.items())从字典中创建了数据帧
Lineouts = []
Games = []
lpg = {}
def Our_Lineouts_Won():
global Lineouts
global lpg
Lineouts = (0)
lpg = {}
for l in my_stats:
t=(l['stats']['data'])
games = (l['_id'])
Games.append(str(l['_id']))
#print (games)
for i in t:
try:
if ((i['key']) == 'Lineouts' and (i['possession'])== 'for'and ((i[('Lineout Presentation')])== 'Off the Top' or 'Scrappy' or 'Drive' )):
#if ((i['key']) == 'Lineouts' and (i['possession'])== 'for'and ((i[('Lineout Presentation')])== 'Off the Top' or (i[('Lineout Presentation')])== 'Scrappy' or (i[('Lineout Presentation')])== 'Drive' )):
Lineouts += 1
except KeyError:
pass
L1=(str(Lineouts))
lpg.update({games:L1})
Lineouts = (0)
#print (lpg)
Our_Lineouts_Won()