python - 从 JSON 文件中提取值并在 Python 中执行计算
问题描述
我目前有一个脚本,它从 JSON 文件中提取值并将它们保存为变量。但是,在保存为变量之前,我需要将所有这些值乘以 100。
with open("match.json",'r') as f:
data = json.load(f)
f.close()
coords = ([(k['X'], k['Y']) for k in data['a']])
x_coords = ([(k['X']) for k in data['a']])
y_coords = ([(k['Y']) for k in data['a']])
这是我正在使用的 JSON 文件。
{'h': [{'id': '310295',
'minute': '6',
'result': 'SavedShot',
'X': '0.8280000305175781',
'Y': '0.639000015258789',
'xG': '0.04247729107737541',
'player': 'Anthony Martial',
'h_a': 'h',
'player_id': '553',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': None,
'lastAction': 'None'},
{'id': '310297',
'minute': '17',
'result': 'Goal',
'X': '0.885',
'Y': '0.5',
'xG': '0.7611688375473022',
'player': 'Marcus Rashford',
'h_a': 'h',
'player_id': '556',
'situation': 'Penalty',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': None,
'lastAction': 'Standard'},
{'id': '310298',
'minute': '21',
'result': 'BlockedShot',
'X': '0.865999984741211',
'Y': '0.6409999847412109',
'xG': '0.08496686071157455',
'player': 'Anthony Martial',
'h_a': 'h',
'player_id': '553',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Paul Pogba',
'lastAction': 'HeadPass'},
{'id': '310299',
'minute': '29',
'result': 'MissedShots',
'X': '0.7719999694824219',
'Y': '0.63',
'xG': '0.031568706035614014',
'player': 'Anthony Martial',
'h_a': 'h',
'player_id': '553',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Jesse Lingard',
'lastAction': 'Pass'},
{'id': '310300',
'minute': '31',
'result': 'MissedShots',
'X': '0.92',
'Y': '0.435',
'xG': '0.05561231076717377',
'player': 'Paul Pogba',
'h_a': 'h',
'player_id': '1740',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'Head',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Anthony Martial',
'lastAction': 'Aerial'},
{'id': '310309',
'minute': '53',
'result': 'BlockedShot',
'X': '0.815',
'Y': '0.519000015258789',
'xG': '0.057843536138534546',
'player': 'Marcus Rashford',
'h_a': 'h',
'player_id': '556',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Harry Maguire',
'lastAction': 'Pass'},
{'id': '310311',
'minute': '64',
'result': 'Goal',
'X': '0.9619999694824218',
'Y': '0.5329999923706055',
'xG': '0.6098462343215942',
'player': 'Anthony Martial',
'h_a': 'h',
'player_id': '553',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Andreas Pereira',
'lastAction': 'Cross'},
{'id': '310312',
'minute': '66',
'result': 'Goal',
'X': '0.8219999694824218',
'Y': '0.4759999847412109',
'xG': '0.37157538533210754',
'player': 'Marcus Rashford',
'h_a': 'h',
'player_id': '556',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Paul Pogba',
'lastAction': 'Throughball'},
{'id': '310315',
'minute': '79',
'result': 'BlockedShot',
'X': '0.84',
'Y': '0.6020000076293945',
'xG': '0.11250953376293182',
'player': 'Marcus Rashford',
'h_a': 'h',
'player_id': '556',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Paul Pogba',
'lastAction': 'Pass'},
{'id': '310316',
'minute': '80',
'result': 'Goal',
'X': '0.88',
'Y': '0.41200000762939454',
'xG': '0.13182462751865387',
'player': 'Daniel James',
'h_a': 'h',
'player_id': '5595',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Paul Pogba',
'lastAction': 'Pass'},
{'id': '310318',
'minute': '86',
'result': 'BlockedShot',
'X': '0.8669999694824219',
'Y': '0.47299999237060547',
'xG': '0.11503136157989502',
'player': 'Mason Greenwood',
'h_a': 'h',
'player_id': '7490',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Aaron Wan-Bissaka',
'lastAction': 'Cross'}],
'a': [{'id': '310293',
'minute': '3',
'result': 'ShotOnPost',
'X': '0.835999984741211',
'Y': '0.38599998474121094',
'xG': '0.03392893448472023',
'player': 'Tammy Abraham',
'h_a': 'a',
'player_id': '702',
'situation': 'FromCorner',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Mateo Kovacic',
'lastAction': 'BallTouch'},
{'id': '310294',
'minute': '3',
'result': 'MissedShots',
'X': '0.7630000305175781',
'Y': '0.64',
'xG': '0.022202739492058754',
'player': 'Pedro',
'h_a': 'a',
'player_id': '687',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Mateo Kovacic',
'lastAction': 'Pass'},
{'id': '310296',
'minute': '11',
'result': 'SavedShot',
'X': '0.7390000152587891',
'Y': '0.44299999237060544',
'xG': '0.021734273061156273',
'player': 'Mason Mount',
'h_a': 'a',
'player_id': '7768',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Pedro',
'lastAction': 'Pass'},
{'id': '310301',
'minute': '34',
'result': 'MissedShots',
'X': '0.7269999694824218',
'Y': '0.46599998474121096',
'xG': '0.01919102855026722',
'player': 'Ross Barkley',
'h_a': 'a',
'player_id': '592',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Pedro',
'lastAction': 'Pass'},
{'id': '310302',
'minute': '35',
'result': 'SavedShot',
'X': '0.7559999847412109',
'Y': '0.355',
'xG': '0.0167279914021492',
'player': 'Pedro',
'h_a': 'a',
'player_id': '687',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'LeftFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'César Azpilicueta',
'lastAction': 'BallRecovery'},
{'id': '310303',
'minute': '38',
'result': 'SavedShot',
'X': '0.9119999694824219',
'Y': '0.6659999847412109',
'xG': '0.06690332293510437',
'player': 'Ross Barkley',
'h_a': 'a',
'player_id': '592',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'LeftFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Pedro',
'lastAction': 'Pass'},
{'id': '310304',
'minute': '38',
'result': 'BlockedShot',
'X': '0.799000015258789',
'Y': '0.4759999847412109',
'xG': '0.039041295647621155',
'player': 'Jorginho',
'h_a': 'a',
'player_id': '1389',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': None,
'lastAction': 'Rebound'},
{'id': '310305',
'minute': '39',
'result': 'ShotOnPost',
'X': '0.9390000152587891',
'Y': '0.6759999847412109',
'xG': '0.05201100930571556',
'player': 'Emerson',
'h_a': 'a',
'player_id': '1245',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'LeftFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': None,
'lastAction': 'None'},
{'id': '310306',
'minute': '45',
'result': 'MissedShots',
'X': '0.92',
'Y': '0.524000015258789',
'xG': '0.043574485927820206',
'player': 'Kurt Zouma',
'h_a': 'a',
'player_id': '935',
'situation': 'FromCorner',
'season': '2019',
'shotType': 'Head',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Mason Mount',
'lastAction': 'Aerial'},
{'id': '310307',
'minute': '50',
'result': 'BlockedShot',
'X': '0.7730000305175782',
'Y': '0.37099998474121093',
'xG': '0.024473881348967552',
'player': 'Pedro',
'h_a': 'a',
'player_id': '687',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'LeftFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Ross Barkley',
'lastAction': 'Pass'},
{'id': '310308',
'minute': '51',
'result': 'BlockedShot',
'X': '0.9330000305175781',
'Y': '0.48700000762939455',
'xG': '0.0619838684797287',
'player': 'Tammy Abraham',
'h_a': 'a',
'player_id': '702',
'situation': 'FromCorner',
'season': '2019',
'shotType': 'Head',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Mason Mount',
'lastAction': 'Aerial'},
{'id': '310310',
'minute': '55',
'result': 'SavedShot',
'X': '0.84',
'Y': '0.715',
'xG': '0.24400267004966736',
'player': 'Emerson',
'h_a': 'a',
'player_id': '1245',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'LeftFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': None,
'lastAction': 'Aerial'},
{'id': '310313',
'minute': '69',
'result': 'MissedShots',
'X': '0.9119999694824219',
'Y': '0.42900001525878906',
'xG': '0.029906723648309708',
'player': 'Kurt Zouma',
'h_a': 'a',
'player_id': '935',
'situation': 'SetPiece',
'season': '2019',
'shotType': 'Head',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Mason Mount',
'lastAction': 'Aerial'},
{'id': '310314',
'minute': '70',
'result': 'SavedShot',
'X': '0.8290000152587891',
'Y': '0.28299999237060547',
'xG': '0.02788177877664566',
'player': 'Emerson',
'h_a': 'a',
'player_id': '1245',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'LeftFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Pedro',
'lastAction': 'Pass'},
{'id': '310317',
'minute': '84',
'result': 'SavedShot',
'X': '0.7819999694824219',
'Y': '0.6480000305175782',
'xG': '0.02486059069633484',
'player': 'Mason Mount',
'h_a': 'a',
'player_id': '7768',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Pedro',
'lastAction': 'Pass'},
{'id': '310319',
'minute': '87',
'result': 'BlockedShot',
'X': '0.8209999847412109',
'Y': '0.6430000305175781',
'xG': '0.04654185473918915',
'player': 'Christian Pulisic',
'h_a': 'a',
'player_id': '2662',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'RightFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Mason Mount',
'lastAction': 'Pass'},
{'id': '310320',
'minute': '88',
'result': 'MissedShots',
'X': '0.9569999694824218',
'Y': '0.5570000076293945',
'xG': '0.27949726581573486',
'player': 'Pedro',
'h_a': 'a',
'player_id': '687',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'OtherBodyPart',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Emerson',
'lastAction': 'Cross'},
{'id': '310321',
'minute': '93',
'result': 'SavedShot',
'X': '0.850999984741211',
'Y': '0.7',
'xG': '0.043492574244737625',
'player': 'Emerson',
'h_a': 'a',
'player_id': '1245',
'situation': 'OpenPlay',
'season': '2019',
'shotType': 'LeftFoot',
'match_id': '11652',
'h_team': 'Manchester United',
'a_team': 'Chelsea',
'h_goals': '4',
'a_goals': '0',
'date': '2019-08-11 16:30:00',
'player_assisted': 'Christian Pulisic',
'lastAction': 'Pass'}]}
目前,我的脚本会将 X 和 Y 值保存为“0.8280000305175781”和“0.639000015258789”,但是我需要将它们乘以 100,例如:“82.80000305175781”和“63.9000015258789”。
任何帮助,将不胜感激。
谢谢!
解决方案
如果您了解您使用的列表推导式,则修改起来非常简单。
y_coords = ([float((k['Y'])) * 100 for k in data['a']])
您应该能够为其他示例执行此操作
推荐阅读
- mathematical-optimization - 如何制定线性规划的初始投资?
- identityserver4 - IdentityServer4 请求太长
- node.js - 节点持久化:initSync 不是函数
- java - eclipse google cloud from appengine sdk迁移问题
- specman - Specman e:有没有办法知道枚举类型中有多少个值?
- java - JavaFX 媒体播放器不播放视频
- beautifulsoup - BeautifulSoup 从锚标记内的脚本中获取文本
- cocoa - 在可可应用程序的不同视图控制器上启用或禁用菜单项?
- java - com.example.daoImpl.FileDaoImpl 中的字段 entityManagerFactory 需要找不到类型为“javax.persistence.EntityManagerFactory”的 bean
- ios - 需要检查我的数组中的特定键值